• 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
1
Question by RyaNNN · Dec 14, 2012 at 03:40 PM · mouserotatelook

Mouse look/rotate

I need help with making script that will rotate character left and right based on Mouse Y axis movement and that will look up and down based on Mouse X axis movement. I've tried several methods including Ray, Quaternion and transform.rotate and nothing works for my problem.

Comment
Add comment · Show 5
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 toum · Dec 14, 2012 at 03:52 PM 1
Share

Could you post a sample of your code?

avatar image bompi88 · Dec 14, 2012 at 07:02 PM 1
Share

have you tried the $$anonymous$$ouseLook script which comes with unity?

avatar image RyaNNN · Dec 14, 2012 at 09:06 PM 1
Share

Tried this for mouse rotation

var speed = 5;

function Update () { if(Input.GetAxis("Horizontal")) {transform.Rotate(Vector3.right speed Time.deltaTime);}

}

It should rotate object only on horizontal axis, but ins$$anonymous$$d it rotates obj on both horizontal and vertical axis, but inverted. When I move mouse forward it rotates to right etc.

avatar image bompi88 · Dec 15, 2012 at 12:28 AM 1
Share

Have you tried using Vector3.up ins$$anonymous$$d of Vector3.right?

avatar image RyaNNN · Dec 15, 2012 at 01:12 AM 1
Share

Tried, still same problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by clunk47 · Dec 15, 2012 at 01:18 AM

Why would you want the mouseX(left and right) to look up and down? and mouseY(up and down) to look left and right??? For one, there's a built in Mouselook script in the CharacterController asset for unity. Horizontal Axis Input is for "a" and "d" and left and right arrows. Vertical Axis is "w" and "s", and up and down arrows. You need GetAxis("Mouse X") or GetAxis ("Mouse Y"). And if you use this in an IF statment you need to ask if it that axis is greater or less than zero.

 if(Input.GetAxis("Mouse X") < 0)
     transform.Rotate(Vector3.up) * speed;
 if(Input.GetAxis("Mouse X") > 0)
     transform.Rotate(Vector3.up) * -speed;

That's just an EXAMPLE. There are many ways. I say Vector3.up there because that's the y axis of your player or w/e your rotating, meaning it will rotate left and right.

You could also do it like:

 transform.localEulerAngles.y+= Input.GetAxis("Mouse X") * speed;

Of course in C# you have to put localEulerAngles in a temp var but that's not the question so I'll stick w/ it. To get you started I'll just lay out that you need to use the mouse axes I named above. If you need help beyond that let us know.

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 RyaNNN · Dec 15, 2012 at 01:42 AM 3
Share

$$anonymous$$y bad on X and Y axis, I accidentally switched them, guess that happens when you don't sleep enough. Anyway, thanks for your help but I've already solved my problem.

avatar image Bichetordue · May 21, 2017 at 04:12 PM 0
Share

I creates an error because unity says "Operator '*' cannot be used with a left hand side of type 'void' and a right hand side of type 'float'"

avatar image thehiveissocial Bichetordue · Sep 02, 2019 at 02:09 AM 0
Share

@Bichetordue To resolve the error, you must wrap the (Vector3.up) speed within a parenthesis. So, ((Vector3.up) speed) will produce no error.

avatar image
2

Answer by stratos_kakalis · Mar 06, 2016 at 08:11 PM

Maybe this is too late but for anyone watching this out there you could simply make a "mouseInput" variable (of course you pick the name) and assign it to Input.GetAxis's the set your transform.Rotate(mouseInput); of course im using C# you might have to make some slight modifications to it

Comment
Add comment · Show 4 · 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 stratos_kakalis · Mar 06, 2016 at 05:31 PM 1
Share

forgot to mention that the reason why i suggest this is that it takes about 3-4 lines of code

avatar image blackshtormx · Apr 24, 2016 at 04:13 AM 1
Share

haha, hi future travellers

avatar image leapformula4 · Jun 03, 2016 at 10:40 PM 3
Share

Here's my C# code to make this work (no character controller or rigidbody):

 void Update () {
         float mouseInput = Input.GetAxis("$$anonymous$$ouse X");
         Vector3 lookhere = new Vector3(0,mouseInput,0);
         transform.Rotate(lookhere);
     }

To adjust speed of rotation, adjust Sensitivity in edit > Project Settings > input > $$anonymous$$ouse X

avatar image ryan123rudder leapformula4 · Oct 22, 2016 at 06:45 PM 1
Share

I have been looking for something like this for hours! Thank you so much!!!

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

18 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

Related Questions

Look at mouse 1 Answer

Look around when holding down mousebutton 1 Answer

Movement along X and Z axis... 2 Answers

Mouse look Y axis sensitivity doesn't change 0 Answers

unity2D: make object face mouse 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