玩家在接触游戏对象时停止移动

本文关键字:移动 对象 接触 游戏 玩家 | 更新日期: 2023-09-27 18:12:25

我让玩家在屏幕上不断向下掉落,当玩家与其他游戏对象互动时,我想要破坏这些游戏对象,让玩家继续向下掉落。

但是当玩家击中其他游戏对象时,游戏对象确实会被破坏,但玩家会停止掉落。请帮助建议我做错了什么。

//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);
        }
    }

玩家在接触游戏对象时停止移动

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

1)。将对象标记为"触发器",这样就不会发生真正的碰撞。这样你的玩家就能保持掉下去的速度。你还需要使用OnTriggerEnter而不是OnCollisionEnter

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

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

调整一下垂直速度和水平速度的值,直到感觉合适为止。

还要注意,如果你在Update()中移动某些东西,你可能应该乘以Time.deltaTime的平移,否则你的播放器将在更高的fps下移动得更快。fixeduupdate在固定的时间间隔内被调用,所以你不需要它(这就是为什么它被称为fixed Update)。