【问题标题】:Player stops moving upon contact with a gameObject玩家在与游戏对象接触时停止移动
【发布时间】:2014-07-11 23:23:13
【问题描述】:

我让我的玩家在屏幕上不断向下掉落,当玩家与其他游戏对象交互时,我想销毁这些游戏对象并希望玩家继续掉落。

但是当玩家击中另一个游戏对象时,游戏对象确实会被摧毁,但玩家会停止掉落。请帮助建议我做错了什么。

//Script attached to player:

//x-axis movement speed
    public float playerMoveSpeed = 0.2f;
    //how much force to act against the gravity
    public float upForce = 9.0f;
    //horizontal control
    private float move;

    // Update is called once per frame
    void Update()
    {
        //player left right x-axis movement control
        move = Input.GetAxis("Horizontal") * playerMoveSpeed;
        transform.Translate(move, 0, 0);
    }

    void FixedUpdate()
    {
        //to fight against the gravity pull, slow it down
        rigidbody.AddForce(Vector3.up * upForce);
    }


//Script attached to gameObject to be destroyed on contact with player

void OnCollisionEnter(Collision col)
    {
        //as long as collide with player, kill object
        if (col.gameObject.tag == "Player")
        {
            Destroy(gameObject);
        }
    }

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    第一个应该可以解决您的问题,第二个可能会让您的生活更轻松:)

    1.) 将对象标记为“触发器”,这样就不会发生真正的碰撞。这样一来,您的播放器就应该通过并保持其速度。您还需要使用 OnTriggerEnter 而不是 OnCollisionEnter

    2.) 如果你真的不需要“力”而只是想不断移动玩家,你可以关闭重力并手动设置刚体.速度(我在这里假设 2d):

    void FixedUpdate()
    {
        horizontalVelocity = Input.GetAxis("Horizontal") * playerMoveSpeed;
    
        rigidbody.velocity = new Vector3(horizontalVelocity, verticalVelocity, 0.0f);
    }
    

    只需调整verticalVelocity 和horizo​​ntalVelocity 的值,直到感觉正确为止。

    另外请注意,如果您在 Update() 中移动某些东西,您可能应该将平移乘以 Time.deltaTime,否则您的播放器会在更高的 fps 上移动得更快。 FixedUpdate 以固定的时间间隔调用,因此您不需要它(这就是为什么它称为 Fixed Update)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多