- Home /
How to make a gameobject with rigidbody2D fly through the air?
After I instantiate an object I want it to go flying through the air. I think I'm supposed to use Rigidbody2D.Addforce but I want to launch the object at an angle and so that it "flies through the air" like it's been hit by a baseball bat. Now I only get it to move at a fixed speed in a straight line like it's not affected by gravity.
Here's the code:
rigidbody2D.AddForce(transform.up * force); //What to type in place of transform.up?
Edit: moved the code to void Start() and now it seems it moves the object only once in the specified direction but how would I set the angle still? Let's say I want the object to have a force added to it in an 90 degrees angle, how would I achieve something like this?
Do you mean you want it to fly straight upwards, and fall back down? Like if it was thrown directly above, and gravity would pull it back down?
Not really, I would like the ball to go flying for example in an angle of 90 degrees like it had been hit by a baseball bat (thats the only way I can explain it). Think of how the birds in angry birds fly through the screen when you throw them, only I want to add a force to the instantiated object in a fixed angle, not make them fly when the player touches them.
Answer by Excrubulent · Nov 26, 2013 at 04:19 AM
If you have a vector, and you need another vector at right-angles to it, you'll need a cross product: http://www.mathsisfun.com/algebra/vectors-cross-product.html If you're not familiar with vector math, get familiar with it. A huge amount of video game logic is comprised of vector mathematics.
You'll see from reading that link that you need two starting vectors from which to create the vector you want. To that end, we need to create a new vector that sticks out of (or into) the screen. We'll call the original vector Bat
, the new vector ScreenNormal
and the vector we want ForceDirection
:
ScreenNormal = new Vector3(0,0,1);
ForceDirection = Vector3.Cross(Bat.normalized, ScreenNormal);
rigidbody2D.AddForce(ForceDirection * force);
Bat.normalized
gives us a vector that has the same direction as Bat
, but a length of 1. We need both input vectors to be normalized in order to get a ForceDirection
that has a length of 1. We don't need to normalize ScreenNormal
because it already has a length of 1.
Depending on what direction your Bat
vector is in, this may give you a vector that goes in the opposite direction to the one you want. To fix this, reverse one of the input vectors (eg: ScreenNormal = Vector3(0,0,-1);
) or swap the order of the parameters in Vector3.Cross(lhs, rhs)
. Either way, you will reverse the result and get what you want.
Vector mathematics isn't hard, by the way, it's just a matter of understanding a series of specific rules and knowing little tricks like this to get the desired outcome.
Thanks for the explanation, but I can't get the code you gave me to work in Unity. It says ScreenNormal, ForceDirection and Bat dont exist in the current context, then I moved the vector declarations out of void Start(); then it says: Assets/Scripts/AddForce.cs(7,68): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration for many times. Am I missing something?
Your answer
Follow this Question
Related Questions
add a force at an angle 3d 0 Answers
Add force once when object is created 1 Answer
Rigidbody2D.AddForce fails to add force 0 Answers
Rigidbody2D AddForce only working with large numbers 2 Answers
Jump problem 1 Answer