- Home /
Rigidbody.AddRelativeForce after modifying gameObject's rotation
transform.rotation = GameObject.Find("Player").transform.rotation;
GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * 10f);
If I change an object's rotation and adding forward relative force to it, I find that the object's local forward has rotated accordingly but the force would still be applied in world forward position. Can anyone please explain to me where I was wrong?
Answer by Captain_Pineapple · Sep 08, 2021 at 08:05 AM
Not sure why this happens but as a workaround: if you want to be sure that the vector is rotated correctly you can instead just do it like this:
GetComponent<RigidBody>().AddForce(GameObject.Find("Player").transform.forward * 10f);
As a sidenote: Please avoid using any Find
methods as much as possible. Anything that relies on String comparisons will brick your game as soon as you rename something and is difficult to debug then. It mostly is also not really friendly on performance.
Thanks! I figured that I could use the worldsapce AddForce with transform.forward, but It was werid that the object would not move in respect to its forward direction with AddRelativeForce after changing the rotation. I was hoping to get clarified if I got some concept wrong.
in theory i'd say your approach is correct. What might be an issue is that AddRelative force takes an old cached transform rotation into account instead of the current newly set one.
Your answer
Follow this Question
Related Questions
Have player fall down with platform? 0 Answers
Smooth dashing issues 0 Answers
Freeze Position + collision? 0 Answers
When Movement speed Change once it never change again 1 Answer
Make a object move forwards 1 Answer