Noitom Hi5开发初体验02 移动

移动采用了最简单的触发机制

右手食指点击左手手背上的方向按钮  碰撞触发移动

前后左右是根据目视角度去对应移动的。(这部分是搜的)

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。

  • 19
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值