简体   繁体   中英

Unity C# Player going too fast with Rigidbody2D.AddForce

So I'm trying to make a game in Unity 5 . It's a 2D game and I want my character to automatically go forward. But my problem is that since I use Rigidbody2D.AddForce , it keeps adding force every frame and I don't want that. I want my speed to be at 5.0f , not more or less. Is there a way to set a limit or keep a constant speed?

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

public Rigidbody2D player;

public bool grounded = false;
private bool hasJumped = false;

public float movementspeed = 5.0f;
public float jumpforce = 450.0f;

// Use this for initialization
void Start () {
    player = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {

    player.AddForce(transform.right * movementspeed);

    // (JumpScript)
}

void FixedUpdate(){

    // (JumpScript)

}
}

I am very suprised at the answers here, they are way off. Your problem is that you are adding force in the update, which is called every frame. That means that every frame of the game the object will gain 5 more force and speed up infinitely.

If you want the object to get 5 force and then stay at that speed, just put the add force in the start instead of the update. Like this:

// Use this for initialization
void Start () {
    player = GetComponent<Rigidbody2D>();
    player.AddForce(transform.right * movementspeed);
}

// Update is called once per frame
void Update () {

    // (JumpScript)
}

You could just set the Rigidbody.velocity to 5.

However, sooner or later knowing some physics will come very handy. The absolute minimum is knowing Newton's laws.

In your case, if you are absolutely dedicated to using AddForce() you could use ForceMode.Impulse as the 3rd parameter to give your object an instant push (instead of gradual acceleration). This way you would have to call AddForce() only once, not repeatedly. You can calculate the exact amount of force you need by the formula for Kinetic energy:

Force = 0.5f * Mass * Velocity^2

I'm writing a 2D game like yours in unity too. And I use an if to check the velocity and use another variable for the force like this:

public float acel = 50, speed = 5;
Rigidbody2D rbody;
void FixedUpdate () {
    // As you want to move right, we don't have to calculate the absolute value
    if (rbody.velocity.x < speed)
        rbody.AddForce(Vector2.right*acel);
}

The speed won't always be 5 but maybe that isn't important though.

If you want to use Physics to move your player at a constant speed then you need to apply force at the beginning until the speed you want is reached, only then change force to 0. In order for this to work as expected I would also add a Physic Material to the collider of the character to make sure that no friction is slowing down the character.

PS: If you are creating some sort of infinite runner game I would consider moving and reusing the environment rather than the player.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM