Я новичок в создании игр (и на самом деле кодировал в целом), я сделал сценарий движения игрока, но мое жесткое тело не движется в направлении, с которой камера сталкивается, я знаю, почему это так, но я не знаю, как ее исправить. Я не хочу переписать весь код, но если мне придется, я сделаю это. < /P>
using UnityEngine;
using UnityEngine.InputSystem;
using Unity;
using UnityEditor;
using UnityEngine.UIElements;
using System.Diagnostics;
public class LocoEngine : MonoBehaviour
{
public bool inverty;
float camx;
float camy;
public float sensitivity;
InputAction moveAction;
InputAction lookAction;
public Rigidbody body;
public Transform player;
public Transform cam;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
moveAction = InputSystem.actions.FindAction("Move");
lookAction = InputSystem.actions.FindAction("Look");
camx = cam.rotation.x;
camy = cam.rotation.y;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame goobah gook
void Update()
{
// Moving the player on the X and Z axis
Vector2 moveValueraw = moveAction.ReadValue();
// Increasing the Magnitude of "moveValue" so that our player can slide across the floor. Then converting that into a Vector3
Vector2 moveValue = moveValueraw * 5;
Vector3 movement = new Vector3(moveValue.x, 0, moveValue.y);
// Apply changes
body.AddForce(movement);
// End of Movement
// Start of Looking
Vector2 lookValueraw = lookAction.ReadValue();
// Define lookValue as a variable, and set it's value to the rotation of the camera, plus the unmodified look rotation.
Vector2 lookValue = new Vector2(camx + lookValueraw.x, camy + lookValueraw.y);
// Set camx/y to lookvalue.x/y respectively.
camy = lookValue.y;
camx = lookValue.x;
// Apply sensitivity, Un-Invert Y (since it's inverted by default), and set the camera to this value.
if (inverty == false)
{
player.eulerAngles = new Vector3(camy * sensitivity - camy * sensitivity * 2, camx * sensitivity, 0);
}
else
{
player.eulerAngles = new Vector3(camy * sensitivity, camx * sensitivity, 0);
}
;
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... -is-facing
Как заставить жесткого тела двигаться в направлении, в котором снимается камера? ⇐ C#
Место общения программистов C#
1751811047
Anonymous
Я новичок в создании игр (и на самом деле кодировал в целом), я сделал сценарий движения игрока, но мое жесткое тело не движется в направлении, с которой камера сталкивается, я знаю, почему это так, но я не знаю, как ее исправить. Я не хочу переписать весь код, но если мне придется, я сделаю это. < /P>
using UnityEngine;
using UnityEngine.InputSystem;
using Unity;
using UnityEditor;
using UnityEngine.UIElements;
using System.Diagnostics;
public class LocoEngine : MonoBehaviour
{
public bool inverty;
float camx;
float camy;
public float sensitivity;
InputAction moveAction;
InputAction lookAction;
public Rigidbody body;
public Transform player;
public Transform cam;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
moveAction = InputSystem.actions.FindAction("Move");
lookAction = InputSystem.actions.FindAction("Look");
camx = cam.rotation.x;
camy = cam.rotation.y;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame goobah gook
void Update()
{
// Moving the player on the X and Z axis
Vector2 moveValueraw = moveAction.ReadValue();
// Increasing the Magnitude of "moveValue" so that our player can slide across the floor. Then converting that into a Vector3
Vector2 moveValue = moveValueraw * 5;
Vector3 movement = new Vector3(moveValue.x, 0, moveValue.y);
// Apply changes
body.AddForce(movement);
// End of Movement
// Start of Looking
Vector2 lookValueraw = lookAction.ReadValue();
// Define lookValue as a variable, and set it's value to the rotation of the camera, plus the unmodified look rotation.
Vector2 lookValue = new Vector2(camx + lookValueraw.x, camy + lookValueraw.y);
// Set camx/y to lookvalue.x/y respectively.
camy = lookValue.y;
camx = lookValue.x;
// Apply sensitivity, Un-Invert Y (since it's inverted by default), and set the camera to this value.
if (inverty == false)
{
player.eulerAngles = new Vector3(camy * sensitivity - camy * sensitivity * 2, camx * sensitivity, 0);
}
else
{
player.eulerAngles = new Vector3(camy * sensitivity, camx * sensitivity, 0);
}
;
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79691432/how-do-i-make-a-rigidbody-move-in-the-direction-the-camera-is-facing[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия