如何使用RigidBody2D Velocity阻止我的角色感觉他们在冰上

时间:2016-09-23 10:14:14

标签: c# unity3d

我目前正在使用RB2D速度进行移动,但在我完成按下按钮后我无法停止滑动。感觉就像马里奥的冰级。如何阻止我的角色在任何地方滑动?

我的代码是:

void Movement() {

     if (Input.GetAxisRaw ("Horizontal") > 0.1) {

         GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);

     }
     if (Input.GetAxisRaw ("Horizontal") < -0.1) {

         GetComponent<Rigidbody2D> ().velocity = new Vector2 (-speed, GetComponent<Rigidbody2D> ().velocity.y);

     }
     if (Input.GetAxisRaw ("Vertical") > 0.5 && grounded) {

         GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jumpHeight);

     }
}

我的速度设置为5,jumpHeight为10,我的播放器上有箱式对撞机和RigidBody2D

感谢您提前提供任何帮助

3 个答案:

答案 0 :(得分:1)

您可以使用变换在按键时移动对象,直接移动它而不使用物理。

同样使用力度,当你没有按任何按钮停止移动物体时,你可以将它设置为0.0f。

crabcrabcam现在正在使用:

    void Movement() {

    if (Input.GetAxisRaw ("Horizontal") > 0.1) {

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);

    } else if (Input.GetAxisRaw ("Horizontal") < -0.1) {

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (-speed, GetComponent<Rigidbody2D> ().velocity.y);

    } else if (Input.GetAxisRaw("Horizontal") == 0 && grounded) {

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (Vector2.zero, GetComponent<Rigidbody2D> ().velocity.y);

    }


    if (Input.GetAxisRaw ("Vertical") > 0.5 && grounded) {

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jumpHeight);

    }

}

这使它在空中漂浮,但在接地时它会相应地停止:)

答案 1 :(得分:1)

将else if添加到第二个if语句,然后使用另一个其他语句来停止移动播放器。

if (Input.GetAxisRaw("Horizontal") > 0.1)
{
Move right
}
else if (Input.GetAxisRaw("Horizontal") < -0.1)
{
Move left
}
else
{
Stop moving/Vector2.zero;
}

也许是这样的:

void Movement()
{

    if (Input.GetAxisRaw("Horizontal") > 0.1)
    {

        GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);

    }
    else if (Input.GetAxisRaw("Horizontal") < -0.1)
    {

        GetComponent<Rigidbody2D>().velocity = new Vector2(-speed, GetComponent<Rigidbody2D>().velocity.y);
    }
    else
    {
        GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    }

    if (Input.GetAxisRaw("Vertical") > 0.5 && grounded)
    {

        GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);

    }
}

答案 2 :(得分:0)

您应该使用public float maxSpeed = 10f; public float jumForce = 700f; public Transform groundCheck; float groundRadius = 0.2f; public LayerMask whatIsGrounded; bool grounded = false; void FixedUpdate() { grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGrounded) float move = Input.GetAxis("Horizontal"); rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y); } void Update() { if(grounded && Input.GetKeyDown(KeyCode.Space)) { rigidbody2D.AddForce(new Vector2(0, jumpForce)); } } 。这是一个简单的代码示例:

merge