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 centaurianmudpig · May 08, 2012 at 10:18 AM · rotationtransformquaternionangleeuler

Problem with Quaternion.Euler() when rotating object

Hello,

I am trying to rotate a game object and I am using Quaternion.Euler to work out the rotation angle. However, I cannot get my code to work correctly. I don't understand why, the Unity documentation for the Euler() function is simple enough.

For example: I rotate my game object to the right by 90 degrees. From this rotation I cannot rotate the game object up/down.

I have included the below code, just attach it to a game object and run. I use the new rotation to calculate a world coordinate for the game object to look at.

Please assist.

 using UnityEngine;
 using System.Collections;
 
 public class Rotate : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 rot = Vector3.zero;
         
         if (Input.GetKeyUp(KeyCode.DownArrow)) rot.x = -45;
         else if (Input.GetKeyUp(KeyCode.UpArrow)) rot.x = 45;
         else if (Input.GetKeyUp(KeyCode.LeftArrow)) rot.y = -45;
         else if (Input.GetKeyUp(KeyCode.RightArrow)) rot.y = 45;
         
         if (rot != Vector3.zero) {
             Vector3 eulerAngles = Quaternion.Euler(rot) * -transform.forward;
               Vector3    lookToPosition = transform.position - eulerAngles * 100000;        
 
             transform.LookAt(lookToPosition);
         }
     }
 }
Comment
Add comment · Show 3
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 centaurianmudpig · May 09, 2012 at 06:51 AM 0
Share

It seems I cannot use Quaternion.Euler(). If Y rotation is 90 degrees, trying to rotate on the X rotation (up/down) will calculate the eulerAngle as it's current rotation. Is there another way I can achieve what I am after as above?

avatar image AlucardJay · May 09, 2012 at 07:06 AM 0
Share

try debugging after the key inputs to see if you're getting the rot :

Debug.Log("rot = " + rot);

I don't know if it'll create an issue , but try rena$$anonymous$$g your var to something that is not used in unity e.g. Vector3 eulerAngles => myEulerAngles , userAngle , setAngle , something different =]

avatar image centaurianmudpig · May 09, 2012 at 08:07 PM 0
Share

I can confirm rot is being set correctly, also tried rena$$anonymous$$g the variable to no effect. All I have been able to do is pin point the problem to this line:

Vector3 eulerAngles = Quaternion.Euler(rot) * -transform.forward;

Don't know why it won't work as intended :(

3 Replies

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

Answer by centaurianmudpig · May 22, 2012 at 09:41 PM

See this post: http://forum.unity3d.com/threads/136444-Calculate-a-new-position-based-using-an-angle-and-distance

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 rutter · May 09, 2012 at 10:19 PM

Obviously something's going wrong, there, but I'm afraid I'm not quite clever enough to spot it, just now.

Depending on what exactly you're trying to do, either of these might work:

 //identify resulting point
 //you could call transform.LookAt() with this
 //or Debug.DrawLine(), etc etc
 transform.TransformPoint(Quaternion.Euler(rot) * Vector3.forward);

 //rotate object
 //can specify self-space or world-space to get diff behavior
 transform.Rotate(rot);
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 centaurianmudpig · May 10, 2012 at 07:13 PM 0
Share

Unfortunately I can't use either option. While my example code is quite simple, in practice I am using Physics for rotation. That is why I came up with calculating a point in space, for the AI to turn towards something (even if it is an empty point in space).

Could this be a bug? Based on the lack of response, could be no-one is using the Euler() function this way?

avatar image
0

Answer by DavidBrehl · Jul 27, 2017 at 06:54 PM

Im trying to convert Euler angles to quaternions, but no matter what it sets the rotation to 135, -90, -225 (not what Im aiming for). I have no idea how to use quaternions, so Im stumped. it might be a bug.

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

7 People are following this question.

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

Related Questions

How can I multiply the rotation of a mirrored object? 1 Answer

Make a side of an object LookAT another object 1 Answer

Smooth Y rotation based on X rotation (C#) 0 Answers

Pitch/yaw difference between 2 transforms 1 Answer

Object makes random jumps in rotation 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