摄像机跟随角色身后的代码

private Transform player;

private Vector3 offset;

private float smoothing = 3;
// Use this for initialization
void Start ()
{
player = GameObject.FindGameObjectWithTag(“Player”).transform;
offset = transform.position - player.transform.position;

}

// Update is called once per frame
void LateUpdate ()
{

Vector3 target = player.transform.position + player.TransformDirection(offset);//关键代码

transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);

transform.LookAt(player);

}

player.TransformDirection(offset);//关键代码 这一句是让摄像机永远可以跟随在主角的身后 transformDirection 让世界坐标变成自身方向

你可能感兴趣的:(摄像机跟随角色身后的代码)