• 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
1
Question by husbandofemily · Apr 25, 2012 at 08:38 PM · rotationangle

Finding the relative angle between two gameObjects (+ve and -ve)

Hi, sorry if this is answered elsewhere, I've looked, but can't find what I'm looking for. Basically, I have a 3rd person game, and when the mouse is clicked, a projectile is instantiated at a gameEmpty near the player's hand. So far, so good, but as the projectile spawns as (transform.rotation), it will only be thrown in the direction the player is pointing, making aiming difficult. What I want to happen is to determine the angle in the Y axis difference between the player and camera, and use this angle to alter the rotation of the spawned object, so it's thrown in the direction the camer is looking. I could use the camera.rotation in the instantiate code, but I want to impose limits on the angle (say +/- 20 degrees of forwards, which would need the angle difference. From what I've gleaned from other threads, you can find the angle between two vectors, but I need the relative angle. I don't expect anyone to write the code for me, but any help pushing me in the right direction would be appreciated greatly, thanks :)

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
2
Best Answer

Answer by aldonaletto · Apr 26, 2012 at 02:55 AM

You could use this:

 var cam = Camera.main.transform;
 var angleY = Mathf.Asin(Vector3.Cross(cam.forward, weapon.forward).y) * Mathf.Rad2Deg;

The cross product of two vectors returns a vector perpendicular to the plane formed by them, and its length is proportional to the sine of the angle between the two vectors. Since you want the horizontal angle, you can get the arc-sine of cross.y - better than the length, cross.y conserves the signal, thus you will get positive angles to one side and negative to the other.

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 husbandofemily · Apr 27, 2012 at 01:27 AM 0
Share

Thanks so much Aldo, that worked a charm! I'm not sure if looking straight up and down is affecting the angle readout, but if it is, it's definitely bearable. $$anonymous$$ight just be me ;) Thanks again! Ian

avatar image aldonaletto · Apr 27, 2012 at 03:22 AM 0
Share

I think that looking down and up will effectively affect the angle returned. I suspect that reading the Y coordinate actually returns info about the angle projected in the horizontal plane (perpendicular to Y), thus the angle reported will be larger when you look up or down. If you want a precise measurement, use this:

  var cross = Vector3.Cross(cam.forward, weapon.forward);
  var angle = $$anonymous$$athf.Asin(cross.magnitude * $$anonymous$$athf.Sign(cross.y))* $$anonymous$$athf.Rad2Deg;
cross.magnitude is the actual sine of the angle, but since magnitude is always positive, you lose the side information - but it can be restored multiplying cross.magnitude by the cross.y sign.
avatar image Tehnique · Jul 07, 2013 at 04:55 PM 0
Share

This was extremely helpful, thank you!

Edit: Actually, after some testing, it seems to cause some "!IsFinite(outDistanceAlongView)" error, but I'll try to fix that.

It solved the problem I had for my turret base (that rotates on Y). But now I want to use it for the turret body(that rotates on X). I tried to adapt it, like this:

 var targetDirection = target.position - turretBody.position;
 var angleOnX = $$anonymous$$athf.Sin(Vector3.Cross(targetDirection, turretBody.forward).x) * $$anonymous$$athf.Rad2Deg;

but it's clear that's not the right way.

Basically, I have a turret that is initially rotated in some random direction on Y and X. The turret base rotates on Y only. The turret body is a child of the base and it rotates on X only. When I select a target, the turret base takes care of rotating my turret towards the target on Y. What I want is for the turret body to rotate on X towards the target, while the base rotates on Y. I'm guessing there are some geometrical rules that I am missing...trying to study the subject, but meanwhile any help would be appreciated...

avatar image robertbu · Jul 07, 2013 at 05:27 PM 0
Share

@Tehnique - rather than asking your question as a comment, please open a new question and make a reference to this one. Your issues may or may not be solved by the answer to this question.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Curve player movement 1 Answer

How do i determin if an objects rotation is between 2 values on each axis? 0 Answers

Align GameObject to Terrain angle 2 Answers

find angle between gameobject and world axis? 1 Answer

Add Force to the right of the rigidbody, not right of the screen 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