控制摄像机平滑旋转背后跟随移动
代码 //将这个代码挂在摄像机上即可

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmoothFollow : MonoBehaviour {
    public Transform target;//这个是你要跟随的物体,比如RPG中的主角  或者  赛车游戏中的赛车

    public float height = 3.5f;//这个是设置摄像机的高度
    public float distance = 7;//这个是设置摄像机的距离
    public float smoothSpeed = 1;//这个是平滑移动的速度
	// Use this for initialization
	void Start ()
    {
		
	}
	
	// Update is called once per frame
	void Update ()
    {
        Vector3 targetForward = target.forward;//以x 和 z 所在平面进行旋转
        targetForward.y = 0;

        Vector3 currentForward = transform.forward;
        currentForward.y = 0;

        Vector3 forward = Vector3.Lerp(currentForward, targetForward, smoothSpeed * Time.deltaTime);

        Vector3 targetPos = target.position + Vector3.up * height - forward * distance;
        this.transform.position = targetPos;
        transform.LookAt(target);
	}
}

相关文章: