实现摄像机跟随并旋转

public class FollowCameraController : MonoBehaviour {

 float distance = 5f ;
    float distanceY = 2f ;
  
    Transform target;
    Transform myTransform;

    float velocity = 0.1f;
    float hevi = 0.5f;

    // Use this for initialization
    void Start () {
        //playerModel = RootServer.instance.GetObject(ROOTObject.Player);
        //targetPosition = playerModel.transform.position;
        target = GameObject.Find("PlayerModel").transform;
        myTransform = transform;
    }

    private void LateUpdate()
    {
        FollowTarget();

    }
  
    void FollowTarget()
    {
        //按y轴的旋转
        float targetAngleY = target.eulerAngles.y;
        float curAngle = myTransform.eulerAngles.y;

       
        //旋转中间值
        curAngle = Mathf.SmoothDampAngle(curAngle, targetAngleY, ref velocity, 0.5f);
        float targetHight = target.position.y + distanceY;
       
        float cameraHight = Mathf.SmoothDamp(myTransform.position.y, targetHight,ref hevi, 1f );
        
        Quaternion currentRotation = Quaternion.Euler(0, curAngle, 0);
        myTransform.position = target.transform.position;
        myTransform.position += currentRotation * Vector3.back*distance;
      
        myTransform.position = new Vector3(myTransform.position.x, cameraHight, myTransform.position.z);
        SetRotation(target.position);
    }


    float AngleDitance(float a , float b )
    {
        a = Mathf.Repeat(a, 360);
        b = Mathf.Repeat(b, 360);
        return Mathf.Abs(b - a);
    }

    void SetRotation( Vector3 target )
    {
        Vector3 cameraPos = myTransform.position;
        Vector3 horDir = target - cameraPos;
        //摄像机朝向
        Quaternion yRotate = Quaternion.LookRotation(new Vector3(horDir.x, 0, horDir.z));
        Vector3 relativeOffset = Vector3.forward * distance + Vector3.down * distanceY;
        myTransform.rotation = yRotate * Quaternion.LookRotation(relativeOffset);

    }

    void CameraShake( float intputX , float inputY )
    {


    }

}

你可能感兴趣的:(实现摄像机跟随并旋转)