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 Qvintus · Feb 04, 2014 at 11:25 PM · rotationanglesx-axisaroundlocaleulerangles

I want to rotate object away or towards camera around x-axis

Hi Unity people and consumers.

Currently working on an controller that can grab stuff and manipulate it. I currently have gotten stuck due to a problem with my rotation function. I want it to be able to rotate stuff relatively to how your looking at it.

Which mean that if I drag my mouse away from me I want my object to rotate on the up axis away from me. If I drag it towards me I want it to rotate towards me. I've gotten it to work on the Y axis (left - right) but it just won't work on the X axis (up - down).

Here is a picture that Illustrates which way I want it to rotate. Around X Axis Rotation

.

And here is my (old) rotation function:

 void rotateObject() {
             
         float RotationX = InteractingObject.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * -0.5f;
         float RotationY = InteractingObject.transform.localEulerAngles.x + Input.GetAxis("Mouse Y") * -0.5f;
 
         InteractingObject.transform.localEulerAngles = new Vector3(RotationY, RotationX, 0);
         
 
     }

The cube is the 'InteractingObject', also using my way the rotation gets to a point where it just kinda sticks to the angle and won't move any further. Again only happens when I want to rotate around the X axis. everything around the Y axis works.

I might just be blind to whats in-front of me so please any ideas let me know.

Here is another way I've tried based on an script I've found posted on another question:

 public Quaternion rotateByAxis(float MouseX, float MouseY) {
         Camera mainCam = Camera.main;
         Vector3 camRelativeUp = mainCam.transform.TransformDirection(Vector3.up);
         Vector3 camRelativeRight = mainCam.transform.TransformDirection(Vector3.right);
         
         angleX += Mathf.Round(MouseX);
         angleY += Mathf.Round(MouseY);
         
         Quaternion leftRight = Quaternion.AngleAxis(angleX, camRelativeUp);
         Quaternion upDown = Quaternion.AngleAxis(angleY, camRelativeRight);
         
         Quaternion rotation =  (leftRight * upDown); //Rotates fine on the Y axis no matter how its rotated in the X
                                                     //Rotates on the object local X axis. Rather than using the camera for
                                                     //some reason.            
         return rotation;
     }

However this seems to work based on which way you decide to multiply it... if you put leftRight upDown the left to right works and upDown is fixed to a local axis. While the other way around (upDown leftRight) the X axis works but left to right is fixed at an local axis. It seems no matter what I do I'll get these problems. Really hoping to see an answer on this one.

Another Update:

 InteractingObject.transform.RotateAroundLocal(new Vector3(Input.GetAxis("Mouse Y"), -Input.GetAxis("Mouse X"), 0), Time.deltaTime * rotationSpeed);  
 //^ transform.RotateAroundLocal(Vector3 axis, float angle);

This function does what I want but in a some what jerky way, it doesn't rotate directly towards the player view or away, so it still feels wrong but is still towards what I'm looking for. Still looking for an answer so please don't hold back with ideas.

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 Scribe · Feb 06, 2014 at 05:05 AM

Try this out!

 var cam : Transform;
 var InteractingObject : Transform;
 var speed : float = 1.0;
 
 private var vertRotAxis : Vector3;
 private var horizontalRot : float;
 private var verticalRot : float;
 
 function Start() {
     cam = camera.main.transform;
 }
 
 function Update () {
         vertRotAxis = InteractingObject.InverseTransformDirection(cam.TransformDirection(Vector3.right)).normalized;
         horizontalRot = Input.GetAxis("Mouse X") * -speed;
         verticalRot = Input.GetAxis("Mouse Y") * speed;
         InteractingObject.Rotate(vertRotAxis, verticalRot);
         InteractingObject.Rotate(Vector3.up, horizontalRot);
 }

It assumes that you are using the main camera though you can change that.

It uses the cameras horizontal axis and converts it to the local space of the object, then it rotates around that so that it is always a rotation aligned with the vertical of the camera.

Scribe

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 Qvintus · Feb 06, 2014 at 06:22 AM 0
Share

I LOVE YOU. I ended up changing it a bit because yours has fixed axis on the horizontal rotation. However you just fixed my problem completely!

 void rotateObject() {
         //InteractingObject.transform.RotateAround(rayPoint, new Vector3(Input.GetAxis("$$anonymous$$ouse Y"), -Input.GetAxis("$$anonymous$$ouse X"), 0), Time.deltaTime * rotationSpeed);
         
         Vector3 vertRotAxis = InteractingObject.transform.InverseTransformDirection(Camera.main.transform.TransformDirection(Vector3.right)).normalized;
           Vector3 hortRotAxis = InteractingObject.transform.InverseTransformDirection(Camera.main.transform.TransformDirection(Vector3.up)).normalized;
            float horizontalRot = Input.GetAxis("$$anonymous$$ouse X") * -rotationSpeed;
            float verticalRot = Input.GetAxis("$$anonymous$$ouse Y") * rotationSpeed;
            InteractingObject.transform.Rotate(vertRotAxis, verticalRot);
            InteractingObject.transform.Rotate(hortRotAxis, horizontalRot);
     }

This is what I ended out with if anyone stumbles upon this later on.

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

19 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

Related Questions

Making an object realistically rebound off walls 1 Answer

localeulerangles changes global angles? 1 Answer

Pointing object at mouse using raycast only works when object is center screen. 2 Answers

How can I pitch and roll a circular platform without releasing vertical/horizontal input? 1 Answer

How to get inspector rotation values ? 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