• 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 Compressed · Jan 20, 2015 at 07:27 PM · c#rotationinputmouse

Rotating object with a mouse movement

Hi, I am trying to achieve mouse controlled rotation of a game object.

I am doing it very simply like this:

 transform.Rotate(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);

When the player moves the mouse up and down (Y axis on the screen) the object rotates up and down and the same goes for rotating left and right with X axis.

It works good, but the problem is, I only want the object to rotate up and down and to left and right. However with the solution above the object also rotates on its sides (as in twists). Imagine a shift stick in a car, you can rotate it up and down and to the sides, but you can't grab it and twist it like a screw.

I tried to fix the problem by doing this:

 transform.Rotate(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
 transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,transform.localEulerAngles.y, 0);

The fix worked great, the object rotated up, down, left and right no problem (the twisting bug was gone), but it caused another problem instead, the object was no longer able to rotate lower than 90 degress (or 270 on the opposite side) for some reason.

I am completely out of ideas on how to fix this.

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 AcE_fLoOdEr · Jan 21, 2015 at 06:17 AM 0
Share

You know sometimes rotation doesn't work with the mouse. It's like when you try to rotate an object using the rotate tool but then it gets fucked up half way through. I think that's what's happening here.

avatar image AlucardJay · Jan 21, 2015 at 07:44 AM 1
Share

Its called Gimbal Lock : https://www.youtube.com/watch?v=zc8b2Jo7mno

avatar image hav_ngs_ru · Jan 21, 2015 at 02:15 PM 0
Share

@alucardj +1

ScreenToWorldPoint + LookRotation = and you can do it without eulerAngles and it's gimbal lock problem

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Flip over an object (smooth transition) 3 Answers

Keep objects current rotation, with Input Mouse X 0 Answers

[C#]Angle of shooting equale to mouse direction 1 Answer

Rotate object with mouse 1 Answer

Standalone gets mouse input when in background UNET 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges