Unity3d添加简单人物移动

首先在场景中添加一个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<CharacterController>();
	}
	
	// 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来讲相机的视角初始位置设置好。

设置情况如下图所示。

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

设置情况如下图所示。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值