Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tongarr · Nov 26, 2013 at 02:47 AM · rigidbody2daddforceangleflying

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Commander Quackers · Nov 26, 2013 at 04:03 AM 0
Share

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?

avatar image tongarr · Nov 26, 2013 at 12:38 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tongarr · Nov 26, 2013 at 12:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges