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 TheLivingTree · Nov 19, 2013 at 06:05 PM · rotationmouse input

Rotating object based off mouse input

Alright here's some background, I have a simple pickup object script that when the mouse is pressed and an object with a rigidbody is visible it will float the object in front of the player. Along with this I also have a rotate function so that when "r" is pressed the camera freezes position and the object will rotate based on mouse movement... well it should anyway. I have the rotation about the Y axis working its the x and z that are confusing me. Basically what I want is a way to see if the player is parallel to the z axis and rotate the object on the z and when the player is on the x axis rotate it on the x. Because it seems like the rotation is relative to the object itself and not the player. If there is someway to set the objects rotation relative to the player that would be awesome :D

 Here's my humble code: 

var rotationX:float = Input.GetAxis("Mouse X") sensitivityX; var rotationY:float = Input.GetAxis("Mouse Y") sensitivityY;

obj.transform.RotateAroundLocal(Vector3.down, rotationX); obj.transform.RotateAroundLocal(Vector3.forwards, rotationY);

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

Answer by TheLivingTree · Nov 22, 2013 at 12:45 AM

FIXED! here's my code for anyone interested:

  function rotateObject()
         {
      
            //Gets the world vector space for cameras up vector 
            var relativeUp: Vector3  = mainCamera.transform.TransformDirection(Vector3.up);
            //Gets world vector for space cameras right vector
            var relativeRight: Vector3  = mainCamera.transform.TransformDirection(Vector3.right);
      
            //Turns relativeUp vector from world to objects local space
                var objectRelativeUp: Vector3  = obj.transform.InverseTransformDirection(relativeUp);
            //Turns relativeRight vector from world to object local space
                var objectRelaviveRight: Vector3  = obj.transform.InverseTransformDirection(relativeRight);
                
                //Calculate rotation
            rotateBy = Quaternion.AngleAxis(-Input.GetAxis("Mouse X") / obj.transform.localScale.x * sensitivityX, objectRelativeUp)
              * Quaternion.AngleAxis(Input.GetAxis("Mouse Y") / obj.transform.localScale.x * sensitivityY, objectRelaviveRight);
      
              //Finally rotate the object accordingly
              obj.rigidbody.MoveRotation(obj.rigidbody.rotation * rotateBy);
         }

I tweaked this script: http://answers.unity3d.com/questions/299126/how-to-rotate-relative-to-camera-angleposition.html

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 DarkShadowsX5 · Jun 02, 2015 at 12:02 AM 0
Share

Thanks just what I spent hours looking for. I converted this to C# and it worked perfectly except the controls were completely inverted.... I'm sure I can fix that.

avatar image TheLivingTree · Jun 02, 2015 at 12:06 AM 0
Share

Haha, glad my answer of two years ago helped XD

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

17 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

Related Questions

Rotating a camera that is already rotated on Z axis 0 Answers

How to look up and down with mouse 2 Answers

Flip over an object (smooth transition) 3 Answers

Player Rotation relitive to active camera 2 Answers

Make sphere rotate when controlled 7 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