自己在学习unity的课程中遇到了,有的地方还没怎么太理解上去,先做个笔记,顺便看看有没有需要的人。
1.搭建一个小场景,一个需要控制的“人”(添加CharacterController组件),地面和几个遮挡物(添加Collider),比如我搭的小场景:
2.先写一个能获取到我们按下鼠标左键,它的位置座标一定就要发生变化,我们需要获取到移动后的位置对于初始位置做出的位移。
using UnityEngine;
using System.Collections;
public class walk : MonoBehaviour {
GameObject obj;
Vector3 startpos;//定义初始位置
public static Vector3 camDelta;//定义移动后的位置对于初始位置做出的位移
void Start () {
}
void Update () {
//如果我们按下鼠标左键 0表是左键 1表示右键 2表示中间
if(Input.GetMouseButton(0)){
if (startpos == Vector3.left) //第一次按鼠标左键
{
startpos = Input.mou