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 sevensixtytwo · Nov 23, 2014 at 07:01 AM · transformrotatecontrolquaternionslook

Transform.Rotate for Quaternions?

Hullo again. Some background first.

I'm working on a turret stabilization script using independent Quaternions. It works great if the rotations are automated (ie, given a specific aimpoint and such) but now I need the player to be able to control it.

Here's a rough and ready snippet from the code:

 public var pivotX : Transform;
 public var pivotXRotation : Quaternion;
 public var pivotY : Transform;
 public var pivotYRotation : Quaternion;
 
 public var speed : float = 24.0;
 
 function Update() {
 
     pivotX.rotation = pivotXRotation;
     pivotX.localEulerAngles = Vector3(0,pivotX.localEulerAngles.y,0);
     pivotY.rotation = pivotYRotation;
     pivotY.localEulerAngles = Vector3(pivotY.localEulerAngles.x,0,0);
 
     }
     
 function Traverse(aimpoint:Vector3) {
 
     var targetRotation = Quaternion.LookRotation(aimpoint - pivotX.position,pivotX.up);
     pivotXRotation = Quaternion.RotateTowards(pivotXRotation, targetRotation, speed*Time.deltaTime);
 
     }
     
 function Elevate(aimpoint:Vector3) {
 
     var targetRotation = Quaternion.LookRotation(aimpoint - pivotY.position,pivotY.up);
     pivotYRotation = Quaternion.RotateTowards(pivotYRotation, targetRotation, speed*Time.deltaTime);
 
     }

Okay, so what I've tried so far was to switch over to another set of codes when the player assumed control, this time using independent eulerAngle values (like the packaged Mouse Look).

Here's a wee bit of code:

 public var pivotX : Transform;
 public var pivotXRotation : float;
 public var pivotY : Transform;
 public var pivotYRotation : float;
 
 public var speed : float = 24.0;
 
 function Update() {
 
     Control();
 
     pivotX.eulerAngles = Vector3(pivotX.eulerAngles.x,pivotXRotation,pivotX.eulerAngles.z)
     pivotX.localEulerAngles = Vector3(0,pivotX.localEulerAngles.y,0);
     pivotY.eulerAngles = Vector3(pivotYRotation,pivotY.eulerAngles.y,pivotY.eulerAngles.z)
     pivotY.localEulerAngles = Vector3(pivotY.localEulerAngles.x,0,0);
 
     }
     
 function Control() {
 
     pivotXRotation += Mathf.Clamp(Input.GetAxis("Mouse X"),-speed,speed); 
     pivotYRotation -= Mathf.Clamp(Input.GetAxis("Mouse Y"),-speed,speed);
 
     }

It works to a point, but I'm having a hell of a time and a sackload of problems trying to sync the quaternions with the new rotations when switching between them. I assume that using Quaternions exclusively should solve my problem.

So to the question, is there a way to rotate Quaternions on individual axes like Transform.Rotate? Prefereably, it should function similarly to the packaged MouseLook script but any method that works is welcomed. Thanks for the time, folks.

UPDATE: my revised working code according to robertbu:

 public var pivotX : Transform;
 public var pivotXRotation : Quaternion;
 public var pivotY : Transform;
 public var pivotYRotation : Quaternion;
 
 public var speed : float = 24.0;
 
 function Update() {
 
     Control();
     
     pivotX.localEulerAngles = Vector3(0,pivotX.localEulerAngles.y,0);
     pivotY.localEulerAngles = Vector3(pivotY.localEulerAngles.x,0,0);
 
     }
     
 function Control() {
 
     var moveX = Mathf.Clamp(Input.GetAxis("Mouse X"),-speed,speed);
     var moveY = Mathf.Clamp(Input.GetAxis("Mouse Y"),-speed,speed)
     
     currentPosition.pivotXRotation = currentPosition.pivotXRotation * Quaternion.AngleAxis(moveX,currentPosition.pivotX.up);
     currentPosition.pivotYRotation = currentPosition.pivotYRotation * Quaternion.AngleAxis(-moveY,currentPosition.pivotY.right);
 
     }
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 robertbu · Nov 23, 2014 at 07:14 AM

Have you looked at Quaternion.AngleAxis()?

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 sevensixtytwo · Nov 23, 2014 at 07:41 AM 0
Share

It's amazing how I missed that! Couldn't they have named it something like Quaternion.Rotate? xD

Thanks, robert! I managed to implement Quaternion.AngleAxis within a few $$anonymous$$utes and also cleaned up the rest of my code. It works great. Brilliant!

avatar image sevensixtytwo · Nov 23, 2014 at 10:44 AM 0
Share

Getting a new problem, I think. $$anonymous$$y controls for pitch get stupid and sluggish when I look global left or right. They then invert when I look global backward. Any idea how to deal with this?

avatar image sevensixtytwo · Nov 23, 2014 at 11:03 AM 0
Share

nvm, I fixed it by following this thread:

camera relative free orbit

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

26 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 avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

4 rotations WTF 1 Answer

Look Behind Me While Still Moving Forward 1 Answer

Transform.Rotate() problem : Avoid Z-axis rotation ? 0 Answers

Rotation Jumping values (0 to 180) 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