- Home /
AddForce on a rigidBody without drift effect (centrifugal force)
Hello Unity community !
I want a sphere which moove in direction of camera forward. That work but when the speed is too high, the drift effect is too strong. When i turn my camera to the right, the sphere need a couple of second to move on camera.forward.
The huge problem is to compensate the forces, when you turn, to obtain a more reactive gameplay.
Im trying to compensate that with the if(), when cam.transform.forward.x between 0 and 1 we turn to the right, if i increase the force value maybe i can compensate the centrifugal effect.
I have also try Force.Impulse mode
// This script is on my sphere player
void FixedUpdate ()
{
Vector3 mouvment = cam.transform.forward;
if(cam.transform.forward.x != 0) // right if positive, else left
{
rigidbody.AddForce(new Vector3(mouvment.x*30,0,1));
}
rigidbody.AddForce(mouvment * speed * Time.deltaTime);
}
Thanks !
Hello,
First thank you for your answers !
I have try both and it's pretty good, the rotation works without centrifugal force ! At least there is one problem :
I fly during some second after a turn, i dont assign the good magnitude on y axe maybe i have try to put 0 ins$$anonymous$$d of cam.transform.forward.y*save$$anonymous$$agn but it dont solve the problem.
public int speed = 60;
public int maxSpeed = 25;
public GameObject cam;
public float save$$anonymous$$agn;
void FixedUpdate ()
{
Vector3 mouvment = cam.transform.forward;
// I save the magnitude when there is no input (no turn)
if(Input.GetAxis("Horizontal") == 0)
{
save$$anonymous$$agn = rigidbody.velocity.magnitude;
}
else // If i turn, velocity = 0 and reafect the old velocity
{
rigidbody.velocity = Vector3.zero;
rigidbody.velocity = new Vector3(
cam.transform.forward.x*save$$anonymous$$agn,
cam.transform.forward.y*save$$anonymous$$agn,
cam.transform.forward.z*save$$anonymous$$agn);
}
// I add this force to make my sphere going on camera forward everytime.
rigidbody.AddForce(mouvment * speed);
// http://answers.unity3d.com/questions/265810/limiting-rigidbody-speed.html i have try this to limite the snowball effect
if(rigidbody.velocity.magnitude > maxSpeed)
{
rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
}
}
Answer by robertbu · Jul 11, 2014 at 02:48 PM
My first suggestion is to up the drag of the rigidbody. The higher the drag, the faster the old velocity will decay and the sooner the new velocity will take hold. You may have to up the amount of force you apply (speed in your code above) to compensate for the drag.
If that is not fast enough, then you can directly assign the velocity. Something like:
rigidbody.velocity = cam.transform.forward * speed;
This will cause an immediate change in velocity. Note that you don't need multiply by deltaTime inside of FixedUpdate. When inside FixedUpdate, Time.deltaTime will actually be Time.fixedDeltaTime which is a constant.
@Pyassin's suggestion is to only modify the rigidbody.velocity when the camera turns. The code could look like:
float mag = rigidbody.velocity.magnitude;
rigidbody.velocity = cam.transform.forward * mag;
This causes an immediate turn but preserves the speed going into the turn.
Answer by Pysassin · Jul 11, 2014 at 02:35 PM
If you want the thing to turn and remain the speed without it still having a force act on the original rotation (like it would irl) save the magnitude that is before turning then set velocity to 0 and then regive it the velocity in the direction you want with the old magnitude.
Your answer
Follow this Question
Related Questions
collider not working when apply "addForce" high enough 2 Answers
addforce + persp camera movement weirdness 1 Answer
Rolling Ball Code Isn't Working 6 Answers
OnTriggerEnter AddForce 1 Answer
Raycast Hit Question 1 Answer