- Home /
Obtaining Constant Speed with AddForce
Hi, I've recently managed to get my character moving with AddForce However when the character starts moving the speed is slow and when I stop moving the character slides (obviously as it accelerates and decelerates)
I would like to make my starting velocity a multiple of direction.magnitude and the ending velocity zero.
Is this possible?
Thanks
Code :
//On Screen Controls
direction = new Vector3(joyScript.position.x, 0, joyScript.position.y);
if (direction.magnitude > 0.1f)
{
Vector3 rightMovement = right * ps3speed * Time.deltaTime * joyScript.position.x;
Vector3 upMovement = forward * ps3speed * Time.deltaTime * joyScript.position.y;
Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
transform.forward = heading;
//transform.position += rightMovement;
//transform.position += upMovement;
rigidbody.AddForce(rightMovement * direction.magnitude * speed);
rigidbody.AddForce(upMovement * direction.magnitude * speed);
rigidbody.AddForce(transform.forward * direction.magnitude * speed);
playerModel.animation.Play("runforward");
playerModel.animation["runforward"].speed = direction.magnitude;
}
else
{
playerModel.animation.CrossFade("idle");
}
I'd eventually like to add the force on the player to the force of the bullet its producing so the player doesn't run into the bullet when it's fired.
Answer by robertbu · Nov 05, 2013 at 04:20 PM
You can set rigidbody.velocity to anything you want. Your AddForce() calls are a bit puzzling to me. I think you will want:
Vector3 v3 = rightMovement + upMovement + transform.forward;
v3 *= direction.magnitude * speed;
rigidbody.velocity = v3;
Note if your code above is exectued every frame in update, then you'll need to make a substantial increase in 'speed'.
As an alternative, you can substantially increase the Rigidbody.drag and adjust the 'speed' to compensate. Depending on settings, you may have just a bit of acceleration/deceleration.
$$anonymous$$Y code was a shambles I have to be honest, I'm working on implementing your suggestion as I type this I can't see why it wouldn't work so I'll make this the answer it does :)
Thanks
I set the drag to allow for a constant horizontal velocity, is there a way to not let the drag effect my vertical velocity?
Your answer
Follow this Question
Related Questions
Accelerate a rigidbody towards max speed 2 Answers
How to make camera position relative to a specific target. 1 Answer
move 2d character affected by physics with velocity and/or add force 2 Answers
Why does writing to rigidbody.velocity after AddForce stop my rigidbody moving? 4 Answers
Character accelerates while running towards walls. "Sonic effect" 1 Answer