0

В игре от третьего лица сделал поворот персонажа в сторону движения относительно камеры с помощью Quaternion.LookRotation и интерполяции. Проблема в том, что после завершения движения, то есть после того как отпускается кнопка, значение поворота обнуляется. Подскажите как решить проблему

public class PlayerHandler : MonoBehaviour
{
    public float speed = 5;
    private CharacterController characterController;
    private Vector3 moveDirection;
    public new Transform camera;

    void Start() => characterController = GetComponent<CharacterController>();

    private void FixedUpdate() {
        var horizontal = Input.GetAxis("Horizontal") * speed;
        var vertical = Input.GetAxis("Vertical") * speed;
        moveDirection = new Vector3(horizontal, 0, vertical);
        moveDirection = Vector3.ClampMagnitude(moveDirection, speed);
        moveDirection *= Time.deltaTime;

        moveDirection = camera.TransformDirection(moveDirection);
        moveDirection.y = -9.8f;

        Quaternion rotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0, moveDirection.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);

        characterController.Move(moveDirection);

    }

}

1 ответ 1

0
if(horizontal!=0 || vertical!=0) {
    Quaternion rotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0, moveDirection.z));
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);
}
1
  • пожалуйста, постарайтесь оставлять чуть более развёрнутые ответы. дополнить ответ можно, нажав править 29 сен 2022 в 13:31

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.