- Home /
Move object
Hi, I'm trying to make my plane fly to the left using a rigidbody.
This is my code in FixedUpdate:
transform.rigidbody.AddRelativeForce(Vector3.right * 100);
This should basically move it to the right, and it works perfectly when there is no roll. (The red line in the Picture shows that it's "following the rotation").
I'm trying to make the plane go right no matter how much roll/rotation there is (The green line in the picture).
How is this possible?
Thanks, Andreas
Answer by Tomer-Barkan · Nov 30, 2013 at 08:01 PM
You're using AddRelativeForce
, which is exactly that, it adds force relative to the plane's right, instead of world right. The plane's right is always it's right wing, so when it's rolled, it's not exactly right...
You can use rigidbody.AddForce()
instead, which will apply the force to the absolute direction that you use.
You can read here about other's physics-based plane game, though it might be more complicated than what you're trying to achieve.
Edit:
Ok, so now that I understand correctly, you want it to go plane-relative left/right, but only in the X/Z plane, while keeping Y constant.
I suggest you use transform.right to calculate the world direction of your plane's right. Then set the the Y of the result to be 0, and normalize it to be of magnitude 1. Then you have the world direction of your plane's right, but flattened so that the world Y of the vector is 0 (basically a projection on X/Z plane). You can use this with AddForce to move in the direction you want:
Vector3 planeRight = transform.right;
planeRight.y = 0;
rigidbody.AddForce(planeRight.normalized * 100);
Thanks for your answer.
The reason I'm using AddRelativeForce is, that the Y axis should not be in world Space. When the yaw of the plane is 0, AddForce will work, but as soon as the yaw is for example 90 degrees, the plane will actually move bacwards ins$$anonymous$$d of to the right :)
Well, you wanted it to go left regardless of rotation, and addforce will do that. If you want something different please explain what exactly in more details.
Hehe yeah I see, sorry for being unclear.
What I'm trying to achieve is to make the plane go it's local left/right regardless of the yaw of the plane. At the same time it should be world roll (so that the plane will move horizontal left/right regardless of the plane's roll).
I hope that's easier to understand :)
Ok, so if I understand correctly, you want it to go relative left/right, but you want the world Y to always be 0...
If I got it right, this is what I suggest:
Use transform.right to calculate the world direction of your plane's right. Then set the the Y of the result to be 0, and normalize it to be of magnitude 1. Then you have the world direction of your plane's right, but flattened so that the world Y of the vector is 0. You can use this with AddForce to move in the direction you want:
Vector3 planeRight = transform.right;
planeRight.y = 0;
rigidbody.AddForce(planeRight.normalized * 100);
[1]: http://Transform.TransformDirection
Thank you very much :)
I changed rigidbody.AddForce to transform.Translate as it worked better in my case, but the principe was the same :)