移动采用了最简单的触发机制
右手食指点击左手手背上的方向按钮 碰撞触发移动
前后左右是根据目视角度去对应移动的。(这部分是搜的)
1.新建空物体 moveDic 来确定移动的方向
2.代码ChildTransform
public class ChildTransform : MonoBehaviour
{
public Transform same;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
transform.localEulerAngles = new Vector3(0, same.localEulerAngles.y, 0);
transform.localPosition = new Vector3(same.localPosition.x, 0, same.localPosition.z);
}
}
其中的same 拖拽MainCamera(eye) 运行的时候MainCamera(head)会改变层级并隐藏掉,所以不能使用
3.代码Move
public class Move : MonoBehaviour
{
/// <summary>
/// 玩家
/// </summary>
public GameObject player;
/// <summary>
/// 方向
/// </summary>
public Transform dic;
/// <summary>
/// 速度
/// </summary>
public float speed=2;
private bool qian, hou, zuo, you,stop = false;//自己写得。。。就很辣鸡
public static Move Instance;
public bool IsWALL = false;//这两个是用来检测是否移动撞墙了
// Start is called before the first frame update
private void Awake()
{
Instance = this;
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void changeDir(bool bol)//这方法没用。。改变不了布尔值不知道为啥。后来都提出来写了
{
qian = false;
hou = false;
zuo = false;
you = false;
stop = false;
bol = true;
}
public Vector2[] dirV2;
// Update is called once per frame
void FixedUpdate()
{
if (IsWALL)//如果碰墙了就往反方向移动一个距离
{
if (hou)
{
player.transform.Translate(dic.forward * 0.2f);
}
//上
else if (qian)
{
player.transform.Translate(-dic.forward * 0.2f);
}
//左
else if (zuo)
{
player.transform.Translate(dic.right * 0.2f);
}
//右
else if (you)
{
player.transform.Translate(-dic.right * 0.2f);
}
IsWALL = false;
return;
}
//这里是正常移动代码
//后
if (hou)
{
player.transform.Translate(-dic.forward * Time.deltaTime * speed);
//player.transform.position = new Vector3(0, 0, 0);
Debug.Log(player.transform);
}
//上
else if (qian)
{
player.transform.Translate(dic.forward * Time.deltaTime * speed);
}
//左
else if (zuo)
{
player.transform.Translate(-dic.right * Time.deltaTime * speed);
}
//右
else if (you)
{
player.transform.Translate(dic.right * Time.deltaTime * speed);
}
}
public void OnTriggerStay(Collider other)
{
if (other.name == "qian")
{
Debug.Log("qian");
qian = true;
hou = false;
zuo = false;
you = false;
}
else if (other.name == "hou")
{
Debug.Log("hou");
qian = false;
hou = true;
zuo = false;
you = false;
}
else if (other.name == "zuo")
{
Debug.Log("zuo");
qian = false;
hou = false;
zuo = true;
you = false;
}
else if (other.name == "you")
{
qian = false;
hou = false;
zuo = false;
you = true;
Debug.Log("you");
}
}
public void OnTriggerExit(Collider other)
{
if (other.name == "qian")
{
changeDir(stop); Debug.Log("stop");
}
else if (other.name == "hou")
{
changeDir(stop); Debug.Log("stop");
}
else if (other.name == "zuo")
{
changeDir(stop); Debug.Log("stop");
}
else if (other.name == "you")
{
changeDir(stop); Debug.Log("stop");
}
}
}
代码挂载在手指上 方便触发判断
4.在人物身上挂载碰撞和钢体,添加代码 如果碰到墙就调用Move的iswall为true。