How to change the direction of a rigidbody without adding to its speed?
I am a newbie trying to making a soccer game with Unity3D. The ball has a rigidbody attached and is moved using AddForce.
I have added a ‘swerve’ effect to the ball by enabling the user to AddForce to the ball with the input keys for one second following each kick. For example, if the ball is kicked directly ‘up’ the screen and then the left key is held down, the ball moves to the left simulating a swerve.
This is working well, but it adds speed to the ball when I only want the input to change the ball’s direction. So if the ball is kicked directly ‘up’ the screen and the ‘up’ key is held down, the ball travels further than I want it to because speed is being added. Ideally I want there to be no effect at all in this scenario, because the direction wouldn't be changed.
So my question is: how can I add direction only to the rigidbody without altering the speed?
Here is how I am adding swerve in FixedUpdate():
if (shallApplySwerve) // shallApplySwerve is true for 1 second after a kick
{
Vector3 inputV3 = new Vector3(matchInputManager.Input.x, 0, matchInputManager.Input.y); // create a Vector3 from input
Vector3 swerveForce = swerveCapability * inputV3; // swerveCapability is a float to determine how much swerve to add
rb.AddForce(swerveForce, ForceMode.Impulse);
}
Any help would be appreciated.
Answer by Doctor_ED · May 22, 2019 at 07:08 PM
Hmmm. If I understand correctly, then after you kick a ball, you want to be able to alter it's flight with "left" and "right" keys for 1 second.
If so, then instead of making a Vector3, you can just add force to the "side" of the ball :
[SerializeField]
private float swerveForce; //some kind of force multiplier, for adjustments
if (shallApplySwerve) // shallApplySwerve is true for 1 second after a kick
{
rb.AddForce(rb.transform.right * matchInputManager.Input.x * swerveScale, ForceMode.Impulse);
}
I'm assuming that matchInputManager.Input.x
is just Input.GetAxis("Horizontall")
...
Thanks for the answer @Doctor_ED but that still increases the velocity of the ball. $$anonymous$$y code already alters the ball flight, but it adds force as well which I don't want. I just want to alter its direction without giving it any extra impetus. I suspect I shouldn't be using AddForce because as the name implies, it adds force. I need something like an 'AddDirection' which wouldn't add any force!
Well, rigidbody tries to mimic real life physic. If a force is already applied to rigidbody, then it just have some velocity in given direction (vector). In theory, to change it (to be "physically acurate") you should use another force, but...
In unity, you can alter velocity and, for e.g. change it's direction. So to achieve what I think you want to, you can use rb.velocity = rb.transform.forward * rb.velocity.magnitude;
on a ball, and then you can use keys, to change only ball rotation. This way the velocity will "follow" the rotation of a ball, giving a, I hope, desired result.
I tried this but it didn't work. I added rb.velocity = rb.transform.forward * rb.velocity.magnitude;
after each kick and then in FixedUpdate() for one second I did rb.AddTorque(rb.transform.right * matchInput$$anonymous$$anager.Input.x * swerveScale, Force$$anonymous$$ode.Impulse);
to change the ball rotation. The ball did not move at all though.