Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NerdRageStudios · May 17, 2017 at 08:18 AM · quaternionforceangle

Converting angle and force, into a force applied to X and Y

Hi, I really need some help with this one! I need to apply force to a character, but because I use my 2D controller, not physics, I apply my forces in X & Y based on the input. However, I need to apply recoil kickback from the characters gun, in the opposite direction that the gun is facing.

I have the rotation of the transform for the gun, its calculated as per below.

 Vector2 mousePos = Input.mousePosition;
 Vector2 screenPos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, transform.position.z - Camera.main.transform.position.z));
 gun.transform.rotation = Quaternion.Euler(0,0,Mathf.Atan2((screenPos.y - transform.position.y), (screenPos.x - transform.position.x)) * Mathf.Rad2Deg);

So my question is, how can I translate that into a force to be applied to the X & Y of the parent object.

For example, if the angle is 45 degrees and I want to apply 20 force. How do I work out how to split that force between the X & Y coords, based on the angle above?

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

1 Reply

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

Answer by NoBrainer-David · May 17, 2017 at 08:39 AM

Assuming you have the GameObject of the character in "character", you can use the dot product to calculate those force magnitudes, like so:

 float xForce = force * Vector3.Dot(character.transform.up, -gun.transform.right);
 float yForce = force * Vector3.Dot(character.transform.right, -gun.transform.right);

This assumes that "right" is forward for your gun but it might be "up", depending on your set up.

A little more detail: The dot-product gives you the magnitude of vector A projected onto vector B. So in our case, we want to project the reverse direction the gun is pointing at onto the forward and right direction of the character. In a 2D scene, the z-axis (which is usually forward) is not used, so right or up becomes forward.

Comment
Add comment · Show 4 · 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 NerdRageStudios · May 17, 2017 at 08:49 AM 0
Share

Thanks very much, I tried that but it doesnt work, it pushes all in the same direction, irrespective of whether I use right or up... im using 2D so its only X & Y and Z for rotation, if that makes any difference?

avatar image NoBrainer-David NerdRageStudios · May 17, 2017 at 09:05 AM 0
Share

Okay, so I'm guessing that your gun and your character might always face the same direction? And your input is relative to the world space and not the character?

Then you could try to use Vector3.up and Vector3.right, ins$$anonymous$$d of character.transform.up and character.transform.right.

avatar image NerdRageStudios · May 17, 2017 at 09:02 AM 0
Share

Actually it works perfectly, it helps when you arent a muppet when implimenting the solution given to you ;)

Thanks man, really appreciate the help :)

avatar image NoBrainer-David NerdRageStudios · May 17, 2017 at 09:07 AM 0
Share

Hey, no problem!

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

66 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

Related Questions

How to do relativte offset rotation 0 Answers

Random perpendicular vector3 after rotation 1 Answer

How can I set a ConstantForce's force to move the object towards another object? 1 Answer

How do I add force to an object based on my Camera's cureent Y rotation (C#) 2 Answers

Quaternion and rotation angles 2 Answers


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