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
1
Question by marius.treu · Aug 22, 2012 at 04:07 PM · rotationlookateulerangles

lookAt euler angles

Hi,

my question is: Is there any way to get the eulerAngles that are required to look at another GameObject without performing the Lookat().

In other words, I want the angles as Vector that I can modify without performing a lookat.

It should look like this: Vector3 lookAtRotation = GetLookAtEulerAngles(gameobject2);

Kind regards

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

Answer by Fattie · Aug 22, 2012 at 04:48 PM

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html

if you want the euler angles...

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-eulerAngles.html

Hi since you're a new user could you please TICK any correct answers, cheers

Comment
Add comment · Show 3 · 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 marius.treu · Aug 22, 2012 at 06:20 PM 0
Share

This is pretty close to what I'm searching for, but ins$$anonymous$$d of an quaternion, I want the euler angles.

First link: Working with quaternions, don't know how to get out the correct eulerangles. Second link: You create a empty quaternion, to set angles. That's heading the wrong direction ^^

avatar image marius.treu · Aug 22, 2012 at 07:00 PM 0
Share

Sorry but still don't get it :(. Lets do an example:

-Step1:GameObject one (position=(0,0,0)) and two (position=(1,1,1)) -Step2:Calculation direction vector (1,1,1)-(0,0,0)=(1,1,1) -Step3:Get out of this direction vector the angles where the vector is pointing to WITHOUT using quaternions - that's what I can't get ahead.

avatar image myama · May 11, 2015 at 04:36 PM 0
Share

Fattie's answer is correct. For your example where direction is (1,1,1):

 // This gives the answer as a quaternion
 Quaternion q = Quaternion.LookRotation(direction);

 // to get the angles just access eulerAngles on the quaternion which LookRotation returned:
 Vector3 angles = q.eulerAngles;

avatar image
1

Answer by Cool Dave · Aug 22, 2012 at 05:15 PM

I don't know exactly how to do that, but here's some snippets I grabbed from an AI script of mine. I had had a very similar problem. If you figure it out, post it here.

 xDistance = target.position.x - carCenter.position.x;
 zDistance = target.position.z - carCenter.position.z;
 var angle : float = Mathf.Atan2(xDistance, zDistance) * Mathf.Rad2Deg;
 carAngle = carCenter.eulerAngles.y;
 steering = (angle - carAngle);
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 marius.treu · Aug 22, 2012 at 06:15 PM 0
Share

Your example is in 2D-Space, but works fine I think. I did the same for 3D and works anyway. But there must be a shorter way or an default method to call.

Thx anyway!

avatar image Dvionst · Feb 24, 2018 at 07:28 AM 0
Share

Thank You very much for this, im searching this for hour. but after tried it. its following at the opposite angle of 180.. so i modified the code a bit like this

 var xDistance = target.position.x - transform.position.x;
 var yDistance = target.position.y - transform.position.y;
 float angle = $$anonymous$$athf.Atan2(xDistance, yDistance) * $$anonymous$$athf.Rad2Deg;
 var targetAngle = target.eulerAngles.z;
 var result = (targetAngle - angle);
 transform.eulerAngles = new Vector3(0f, 0f, result+180f);

avatar image
0

Answer by marius.treu · Aug 22, 2012 at 07:02 PM

Extrension:

To solve this problem I usually calculate the distance vector and calculate each axis x,y,z with sin / cos. That's kind of a roundabout way. There must be an defined method!

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

9 People are following this question.

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

Related Questions

LookAt script Issues 0 Answers

How do I make transform.Lookat use Vector3.down instead of Vector3.forward? 1 Answer

Trying to have object spin within player and always face "up" relative to camera [example pics of what I'm trying to achieve in post]] 0 Answers

LookAt on only 1 axis for 2D games? 1 Answer

Quaternion Equivalent of restricting an axes rotation to zero 3 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