本篇文章和大家介绍的是摄像机跟随人物,
这里的视角主要是从人物的背后跟随的。
首先新建一个C#脚本,命名为MyFollow,然后把以下代码粘贴进去,保存:
AخA
1
using UnityEngine;
2
using System.Collections;
3
public class MyFollow : MonoBehaviour
4
{
5
public float distanceAway = 5; // distance from the back of the craft
6
public float distanceUp = 2; // distance above the craft
7
public float smooth = 3; // how smooth the camera movement is
8
private GameObject hovercraft; // to store the hovercraft
9
private Vector3 targetPosition; // the position the camera is trying to be in
10
Transform follow;
11
void Start(){
12
follow = GameObject.FindWithTag ("Player").transform;
13
}
14
void LateUpdate ()
15
{
16
// setting the target position to be the correct offset from the hovercraft
17
targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
18
// making a smooth transition between it's current position and the position it wants to be in
19
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
20
// make sure the camera is looking the right way!
21
transform.LookAt(follow);
22
}
23
}
接着把上面的这个脚本挂载到摄像机上。
最后把你想跟随的人物的tag设为Player。
运行游戏后,摄像机就可以从人物背后跟随人物了。