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 AndreasX12 · Dec 07, 2013 at 09:29 PM · rotationrotateobjectsangletwo

Rotation angle from one object to another

Hi,

I'm trying to ge the rotation angle from my transform to another object.

Here's a little picture to describe what I mean alt text

What I'm trying to achieve is the following: Get the rotation angle from my transform to another gameobject. So that 0° is forward of my transform (The biggest cube in this case). C#

Please let me know if I haven't explained this good enough :)

Thanks, Andreas.

angle.png (211.5 kB)
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

5 Replies

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

Answer by diegzumillo · Dec 07, 2013 at 09:40 PM

If I get what you're asking this is a simple task. You want the angle between the direction the first cube is looking and the position of the other cube? Then it's something like

 Vector3.AngleBetween( object 1 forward, object 1 position - object 2 position  );
Comment
Add comment · Show 6 · 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 AndreasX12 · Dec 07, 2013 at 10:13 PM 0
Share

Thank you very much :D I knew there were a simple way to achieve this.

avatar image diegzumillo · Dec 07, 2013 at 10:19 PM 0
Share

Did you try it out? is it what you wanted? :)

avatar image Hexxon · Apr 09, 2017 at 04:51 PM 0
Share

I tried using this function, but it always returns a positive value, meaning I cant tell if the object is 35° left or right from me, which - in fact - sucks. So i have to calculate this manually... Thanks Unity.

avatar image Bunny83 Hexxon · Apr 10, 2017 at 01:09 AM 0
Share

Any angle between two vectors can be interpreted in two different ways. 35° is the "small" angle and 325° is the "large" angle. Both represent the same angle just the other way round. To define a specific orientation you need to specify a reference frame. Two 3d vectors could be oriented any way in space. Without another reference vector there is no concept of "left" or "right".

avatar image Hexxon Bunny83 · Apr 10, 2017 at 08:13 AM 0
Share

Well, thats your way of interpreting it... I'd say If I especially want to calculate the angle in respect to my forward vector and in 2D only, I'd need an indication if its positive or negative... but whatever. I've come up with this code now:

 // the position to compare with
 var targetPosition = new Vector3();
 // use my gameobject's transform to deter$$anonymous$$e fwd vector to target
 var localTarget = transform.InverseTransformPoint(targetPosition);
 // Use Trig to get my Angle IN RANGE -180 to 180
 var targetAngle = $$anonymous$$athf.Atan2(localTarget.x, localTarget.z) * $$anonymous$$athf.Rad2Deg;
avatar image diegzumillo Hexxon · Apr 10, 2017 at 07:24 PM 0
Share

"left or right of me" I imagine you are working in 2D then. In 3D left/right don't make much sense and it requires more information, maybe a vector itself. Anyway, maybe an easier way is to just use a cross/vector product. It will be a vector pointing in the z direction, and it will be positive or negative depending on whether the vector is 'to the left or right' so to speak.

avatar image
2

Answer by Noxrawr · Dec 07, 2013 at 10:25 PM

Hi, this should work.

 Vector3 direction = (target - transform.position).normalized;
 float angle = Vector3.Angle (direction, unit.transform.forward);
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
0

Answer by UziMonkey · Apr 09, 2017 at 10:44 PM

A simple Vector3.Angle call will work here, but usually when I do this I want to know what the angle between them in relation to some plane. If you were to do this:

 Vector3.Angle(transform.forward, other.position - transform.position);

You will get the angle between the two through a plane defined by, I think, the first point and the cross product between the two vectors. If the objects are always on the same plane, that's fine, it'll be the same thing. However if one object is elevated above the grid in your image then the angle will not be the same, and the further the object is away from the grid plane the more wrong the result of his function will be.

So instead you can do the same thing, but project the vectors onto a know plane, in this case a plane defined by Vector3.up.

 var angle = Vector3.Angle(
   Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized,
   Vector3.ProjectOnPlane(other.position - transform.position, Vector3.up).normalized
 );

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 SpaceManDan · Mar 10, 2021 at 02:46 PM 0
Share

Maybe I'm not understanding this, but wouldn't this start spitting out funky numbers when the forward was facing up?

avatar image
0

Answer by PostleService · Jul 30, 2021 at 05:59 PM

So I keep getting 90 degrees no matter what.

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
0

Answer by astracat111 · Sep 06, 2021 at 10:21 PM

I know this is really dumb, but I just create a dummy object then do LookAt() and then you just subtract the angle of the first gameObject from the dummy gameObject. This wouldn't be good for operating every single frame though, but you could just have a dummy object for general purposes and to LookAt() then just subtract the transform.eulerAngle.y's from each other.

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 SpaceManDan · Sep 09, 2021 at 08:33 PM 0
Share

That's not dumb, that's smart. If it works then no reason to knock it. That being said, having a second object in play opens up for more possibilities for bugs and adds another object to the resource pool. Cost vrs reward so sometimes it's a good plan, and others it's not. Depends on what it is you're trying to do.

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

24 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

Related Questions

Set rotation based on 2 points problem 1 Answer

Move object towards an angle the distance of two points 1 Answer

Set Rotation Angle 1 Answer

Axis are not rotated as expected 0 Answers

How do I call a function containing a while loop, inside the update function? 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