【问题标题】:How can i make a player stop moving when it touchs a collider? [closed]如何让玩家在碰到对撞机时停止移动? [关闭]
【发布时间】:2022-01-19 02:44:01
【问题描述】:

所以我想知道是否有一种方法可以让玩家只需单击一下就可以无限移动,并在与另一个对撞机发生碰撞时停止。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您可以创建一个全局属性:

    bool movePlayer = false;
    

    在 FixedUpdate 方法中,您可以检测鼠标点击并将 movePlayer 更改为 true,如果值为 true,则移动播放器:

    void FixedUpdate() {
        if (Input.GetMouseButtonDown(0))
            movePlayer = true;
    
        if (movePlayer) {
            //move the player here, maybe with Translate method
        }
    }
    

    使用 OnTriggerEnter 方法可以检测碰撞并将 movePlayer 更改为 false:

    void OnTriggerEnter(Collider other) {
        //you can check for some particular object or avoid this if
        if (other.gameObject.name == "SomeObject")
            movePlayer = false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多