题引
这是初学者的自习笔记,错误会有,若有不爽,还请指正流程相关:
1.首先创建地板(plane),并创建一个需要使其停留在地板上的3D物体。如下图
2.编写物体移动代码,和相机的跟随代码,如下
物体移动代码
public float speed;
void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Jump");
float z = Input.GetAxis("Vertical");
transform.Translate(new Vector3(x,y,z)*Time.deltaTime*speed);
}
相机跟随代码
public GameObject CT;
public float Cspeed;
private void FixedUpdate()
{
if (CT != null ) {
transform.position = Vector3.Lerp(transform.position, CT.transform.position,Time.deltaTime * Cspeed);
}
}
3.保存后在Unity有分别给物体和相机挂载脚本并且给挂载的脚本赋值
运行后会发现相机会追踪物体(Cube)的位置,物体在按下移动键后摄像头跟随物体移动