尝试制作一个平滑旋转和移动的相机

问题描述 投票:0回答:1
using UnityEngine;

public class CameraFollow : MonoBehaviour {

    public Transform target;

    public float smoothingspeed_movement = 0.125f;
    public float smoothingspeed_rotation= 0.5f;
    public Vector3 offset;

    void FixedUpdate ()
    {

        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothingspeed_movement);
        transform.position = smoothedPosition;
        
        Vector3 desiredRotation = target.eulerAngles + offset;
        Vector3 smoothedRotation = Vector3.Lerp(transform.rotation.eulerAngles, desiredRotation, smoothingspeed_rotation);
        
        transform.eulerAngles = smoothedRotation;

    

    }

}

我面临的问题是,当相机向左移动时,从旋转0,回到359,它会平滑向右移动,而不是平滑向左。所以它总是进行 360 度旋转。

unity-game-engine vector
1个回答
0
投票

您可以对两个四元数进行球面插值 (slerp),而不是对欧拉角进行插值:旧旋转和新旋转:

Quaternion desiredRotation = target.rotation * Quaternion.Euler(offset);
Quaternion smoothedRotation = Quaternion.Slerp(transform.rotation, desiredRotation, smoothingspeed_rotation);

transform.rotation = smoothedRotation;
© www.soinside.com 2019 - 2024. All rights reserved.