Unity3d添加简单人物移动

Unity3d添加简单人物移动_第1张图片

首先在场景中添加一个cube。

并对其添加一个character controller 组件。因为character controller组件中包含碰撞体,所以可以将原本的collider删除掉。

然后对其添加一个c# script 组件用于编写代码。

其c#代码编写如下所示。

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

public class player : MonoBehaviour {
    private CharacterController controller;
    public int speed = 10;
	// Use this for initialization
	void Start () {
        controller = this.GetComponent();
	}
	
	// Update is called once per frame
	void Update () {

        controller.SimpleMove(new Vector3(Input.GetAxis("Horizontal") * speed, 0, Input.GetAxis("Vertical") * speed));
	}
}

如果要实现相机的跟踪,则首先将工作窗口调整到合适的视角

然后选中main camera。并通过Game Object菜单栏中的 align with view来讲相机的视角初始位置设置好。

设置情况如下图所示。

Unity3d添加简单人物移动_第2张图片

然后为了实现相机的实时跟随,则可以将main camera拖到cube下作为其子物体。这样一来,main camera就转移到了这个cube的坐标系之下。在每一帧刷新的时候都会根据cube的位置调整其相对坐标不变。

设置情况如下图所示。

 

你可能感兴趣的:(Unity学习笔记,unity3d,相机,移动,跟随,c#)