Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 GlassesGuy · Jul 05, 2019 at 08:35 PM · rotationphysicsrigidbodyaddforcerotation axis

Can you add a force to a rigidbody at an angle?

Hi! I am making a game where you control a sphere by adding forces to the rigidbody of said sphere. To accomplish that I would like to be able to add a force to the rigidbody around the y axis of rotation(not relative to the sphere). I can't make this relative to the sphere since it will bump into other objects and the physics engine will rotate the sphere. It's a little confusing so I have an image that might help visualize my idea. Image Link

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by GlassesGuy · Jul 06, 2019 at 01:48 AM

Thanks to @metalted I started looking up equations to turn an angle into x and y coordinates. I found this and adapted it to work with my game. Here's the code

 public void AddForceAtAngle(float force, float angle)
 {
 float xcomponent = Mathf.Cos(angle * Mathf.PI / 180) * force;
 float ycomponent = Mathf.Sin(angle * Mathf.PI / 180) * force;
 
 playerRB.AddForce(ycomponent, 0, xcomponent);
 }

This works great and you can modify the Vector3 for adding a force on your rigidbody and change what plane you want it on.

Comment
Add comment · 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
1

Answer by metalted · Jul 05, 2019 at 09:11 PM

You want to convert an angle to force applied to the x and z direction? You could use cosine and sine for this. This will convert the angle into 2 components that can be applied to the object. When using cos and sin, the 0 degree angle will be at the right side of the object. I hope my explanation is clear, but the code will make it easy to understand. Lets say we want to move the object to the top-right with a force of 500 units. Because the 0 degree angle is at the right side of the object, the top right "corner" of the circle will be at 45 degrees:

 public void AddForceAtAngle(float force, float angle){
     angle *= Mathf.Deg2Rad;
     float xComponent = Mathf.Cos(angle) * force;
     float zComponent = Mathf.Sin(angle) * force;
     Vector3 forceApplied = new Vector3(xComponent, 0 , zComponent);
     
     rb.AddForce(forceApplied);
 }

So lets do the math. If we have an angle of 45 degrees Cos(45) will be 0.707, Sin(45) will be 0.707 as well. Multiplying these values with our force of 500 will give us the following components: x = 353, z = 353. If we calculate the resulting vector (using Pythagoras) we get a value of 500. Because the cos and sin work with any angle, you can also enter a value above 360 degrees and get a good result. Hope this helps!

Comment
Add comment · Show 2 · 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 GlassesGuy · Jul 05, 2019 at 09:38 PM 0
Share

After some testing it doesn't seem to give the same results you said they would. I end up with X= 262.660994409 and Y = 425.451762267. Is there something I'm missing? I ran the numbers in my calculator and got the same numbers I did in Unity.

avatar image metalted GlassesGuy · Jul 08, 2019 at 05:07 AM 1
Share

@GlassesGuy Ahh I see the problem, its a radians degrees issue. Some calculators use degrees, some use radians. I created the answer using my calculator, which uses degrees. Your calculator, and Unity so it seems (oops) make use of radians. To fix this you can simple multiply the angle entered with $$anonymous$$athf.Deg2Rad or you'll have to convert the angle beforehand, but that's not really user friendly. I'll update the code so it will give the right answer when entering an angle in degrees.

avatar image
0

Answer by AonaS · Aug 09, 2021 at 08:22 AM

@metalted Hi, first of all, thanks a lot. Your answer helped me a lot and it works great. I was trying to shoot multiple projectiles and working on your code did it well! However, I only manage to shoot at certain angles in only one direction but I want to shoot at with same angles based on my rotation. How can I keep the angle same based on my rotation ? Can you think of anything to work around this ? This is my first post/comment so I don't know if I should continue here or post another question to forum.

Comment
Add comment · 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

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

220 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Prevent Rigidbody From Rotating 3 Answers

Get result (force & torque) of AddForceAtPosition? 2 Answers

Update speed and physics makes my rigidbody jiggle 2 Answers

Why is force only being added in the same direction? 1 Answer

Player Rotation with the mouse 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