Я новичок в создании игр (и на самом деле кодировал в целом), я сделал сценарий движения игрока, но мое жесткое тело не движется в направлении, с которой камера сталкивается, я знаю, почему это так, но я не знаю, как ее исправить. Я не хочу переписать весь код, но если мне придется, я сделаю это. < /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#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Мне трудно заставить объект вращаться и двигаться в новом направлении вперед.
Anonymous » » в форуме C# - 0 Ответы
- 21 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Перемещение персонажа относительно их местоположения с использованием единства жесткого тела
Anonymous » » в форуме C# - 0 Ответы
- 1 Просмотры
-
Последнее сообщение Anonymous
-