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 AlucardJay · Oct 05, 2012 at 03:34 PM · rotationquaternionlerpeuler

apply a modified rotation based on another objects' rotation

I'm completely lost on quaternion math and applying rotations so here is my question.

How can I get the rotation of object 1, calculate from that a target angle, and then apply that rotation to object 2 ?

here's what I came up with so far :

 public var thePlayer : Transform;
 var speed : float = 8.0;
 var turnSpeed : float = 0.5;
 var startRotY : float = 25.0;
 enum allStates { Idle, Spawning, FlyingForward, TurningToBehind, WaitingToRespawn }
 var currentState : allStates;
 
 function Spawning() 
 {
     // position at relation to player + randomness
     transform.position = thePlayer.position + ( thePlayer.forward * -10.0 ) + ( thePlayer.right * -20.0 );
     
     // rotation at angle to player + randomness
     var customRotation : Quaternion = Quaternion.Euler( thePlayer.rotation.x, RoundAngle( thePlayer.rotation.y + startRotY ), thePlayer.rotation.z );
     
     //this.transform.rotation = thePlayer.rotation;
     transform.rotation = customRotation;
     
     // Switch state
     currentState = allStates.FlyingForward;
 }
 
 function TurningToBehind() 
 {
     // rotate to 180' to player
     var turnAngle : Quaternion = Quaternion.Euler( thePlayer.rotation.x, 
                                                     RoundAngle( thePlayer.rotation.y - 180.0 ), 
                                                     thePlayer.rotation.z );
     
     transform.rotation = Quaternion.Lerp( transform.rotation, turnAngle, turnSpeed * Time.deltaTime );
     
     // keep turning and flying
     transform.position += transform.forward * speed * Time.deltaTime;
     
     //check if position is behind players local Z
     // using Dot.Product here =]
     
     // if behind player , change state
     //currentState = allStates.WaitingToRespawn;
 }
 
 function RoundAngle( theAngle : float ) : float
 {
     if ( theAngle > 360 )
     {
         theAngle -= 360;
     }
     else if ( theAngle < -360 )
     {
         theAngle += 360;
     }
     
     return theAngle;
 }


When I rotate the Player, the Enemy does not notice this or use anything but Euler(0,0,0) while using thePlayer.rotation in any rotation calculation.

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 MountDoomTeam · Oct 05, 2012 at 03:49 PM

 Obj1.transform.rotation = Obj2.transform.rotation * rot;

where rot is a vector3 declared as the eulerAngles of a Quaternion.

just get both objects'transform.rotation, and set one to be equals the other if you want to make them the same, else to add them, store the first one's transform rotation as a variable, and set set the object to a new rotation by multiplying transform rotation of object 1 with that of object 2.

http://answers.unity3d.com/questions/324886/tree-branch-help-cumulation-of-quaternion-rotation.html

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 AlucardJay · Oct 05, 2012 at 03:59 PM 0
Share

see, that's what does my head in =] I found that while searching, but I cannot get my head around multiplying 2 angles gives a result which is actually adding 2 angles. But I appreciate the response , and shall check with this now :

 var turnAngle : Quaternion = thePlayer.rotation * Quaternion.Euler( 0.0, 180.0, 0.0 );
avatar image AlucardJay · Oct 05, 2012 at 04:21 PM 0
Share

Well, it still does my head in, but it really is that simple, wow. When I changed the relevant parts to the below, it worked as expected. $$anonymous$$any thanks =]

 var turnAngle : Quaternion = thePlayer.rotation * Quaternion.Euler( 0.0, 180.0, 0.0 );
 transform.rotation = Quaternion.Lerp( transform.rotation, turnAngle, turnSpeed * Time.deltaTime );
 
avatar image MountDoomTeam · Oct 05, 2012 at 04:46 PM 1
Share

i was on that one for a loooong scratchy head time .... so i didnt even stop to wonder if i knew how it works when i found the solution! it''s just some 3d trickery. $$anonymous$$any of the greatest $$anonymous$$ds of our century have struggled with the question of 3d rotations. it's something to do with matrixes of sin and cos theta. :)

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

10 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

Related Questions

How can I use lerp to rotate an object multiple times? 2 Answers

Quaternion Rotation On Specific Axis Issue 0 Answers

Rotation with a slider 2 Answers

How to represent a gameobject's (x,y,z) rotation as a quaternion? 2 Answers

How to smoothly rotate to certain directions using input axis 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