- Home /
Apply Constant Force
I want to apply constant force to game object which has rigid body attached. But I found that after applying continuous force to game object, game object's speed increased per frame. That I have checked through printing velocity of game object.
I have used following code.
private Vector2 force;
public float speed, downLimit;
void Start ()
{
force = new Vector2 (speed, 0f);
}
void FixedUpdate ()
{
rigidbody2D.AddForce (force);
print ("bird velocity : " + rigidbody2D.velocity);
CheckForDownLimit ();
}
private void CheckForDownLimit ()
{
Vector3 birdPosition = transform.position;
if (birdPosition.y < downLimit * -1f) {
force.y = speed * Constants.VERTICLE_SPEED;
} else if (birdPosition.y > downLimit) {
force.y = -speed * Constants.VERTICLE_SPEED;
} else {
force.y = 0;
}
}
I want to move my game object with same speed at any level of game not with continuous increasing speed.
Answer by mattyman174 · Apr 21, 2014 at 07:46 AM
Just check if the Velocity of your Rigidbody is going to exceed a maximum value and then just set it to that maximum if it does.
Answer by HarshadK · Apr 22, 2014 at 12:52 PM
Instead of using the force you can use MovePosition to move your rigidbody. Using MovePosition will ensure that the physics is also calculated as you want it to be since you are using force.
Thanks for your answer. I got some new point from it. But I want to use physics behaviour so I mark first answer as correct one but up vote your suggestion also.