Контроллер от третьего лица вращается не туда - PullRequest
1 голос
/ 05 февраля 2020

Я пытался сделать контроллер от третьего лица и все идет нормально. Прямо, пока я не начну на самом движении.

Когда ось Y достигает значения, превышающего 360, она вращается полностью от 360> 300..150> 50> 5 вместо всего лишь 360> 5, и я не знаю, как заставить ее делать то, что я хочу

Ниже приведены два сценария, которые я использую

Вращение камеры:

public bool LockMouse;
public Transform target;
public float Sens;
[Range(0,10)]
public float Distance = 2;
public Vector2 MinMaxPitch = new Vector2(-40, 85);

public float RotSmoothTime = 0.12f;
Vector3 rotationSmoothVelocity;
Vector3 currentRot;

float pitch;
float yaw;

private void Start()
{
    if(LockMouse)
        Cursor.lockState = CursorLockMode.Locked;
}

void LateUpdate()
{
    yaw += Input.GetAxis("Mouse X") * Sens * Time.deltaTime;
    pitch -= Input.GetAxis("Mouse Y") * Sens * Time.deltaTime;
    pitch = Mathf.Clamp(pitch, MinMaxPitch.x, MinMaxPitch.y);

    currentRot = Vector3.SmoothDamp(currentRot, new Vector3(pitch, yaw), ref rotationSmoothVelocity, RotSmoothTime);

    transform.eulerAngles = currentRot;

    transform.position = target.position - transform.forward * Distance;
}

Движение игрока:

public float speed;
public float SmoothSpeed;
Transform camT;
Rigidbody rb;
Vector3 rot;
private Vector3 vel = new Vector3(0, 0, 0);

// Start is called before the first frame update
void Start()
{
    camT = Camera.main.transform;
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
    rb.velocity = (transform.right * Input.GetAxis("Horizontal") * Time.deltaTime * speed) + (transform.forward * Input.GetAxis("Vertical") * Time.deltaTime * speed) + (transform.up * rb.velocity.y);

    if(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).magnitude > 0)
    {
        rot = new Vector3(0, camT.eulerAngles.y, 0);
        Debug.Log(rot);
        if(rot.y > 358)
        {
            transform.eulerAngles = new Vector3(rot.x, 3, rot.z);
        }

        if (rot.y < 2)
        {
            transform.eulerAngles = new Vector3(rot.x, 357, rot.z);
        }
    }

    transform.eulerAngles = Vector3.SmoothDamp(transform.eulerAngles, rot, ref vel, SmoothSpeed);

}

1 Ответ

1 голос
/ 05 февраля 2020

Vector3.Smoothdamp не понимает, что, например, (x, 359, z) может обернуться вокруг (x, 0, z).

Используйте Quaternions вместо векторов и используйте Quaternion.RotateTowards, чтобы вычислить вращение, используемое между текущим вращением и rot:

public float speed;
public float smoothSpeed; // naming convention for fields is camelCase
Transform camT;
Rigidbody rb;
Quaternion rot;
private Vector3 vel = new Vector3(0, 0, 0);

// Start is called before the first frame update
void Start()
{
    camT = Camera.main.transform;
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
    rb.velocity = (transform.right * Input.GetAxis("Horizontal") * Time.deltaTime * speed) + (transform.forward * Input.GetAxis("Vertical") * Time.deltaTime * speed) + (transform.up * rb.velocity.y);

    if(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).magnitude > 0)
    {
        rot = Quaternion.Euler(0f,camT.eulerAngles.y,0f);
    }

    transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime * smoothSpeed);

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...