Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by crazygamerdl1 · Mar 11, 2017 at 11:55 PM · camerarotationmousecursorlook

Mouse Look Script Not Working

I am making a script that will make the camera move when my mouse moves. It will also confine the cursor to the center of the screen unless the escape key is pressed. This code is not working, I would really appreciate any advice as to why my script is not working. In case you are wondering, the code checks out but the screen tilts. You can copy this script if you would like to see what I mean by "tilt".

Here is the code:

pragma strict

function Start () { Cursor.lockState = CursorLockMode.Locked; }

var horizontalSensitivity : float = 10; var verticalSensitivity : float = 10;

function Update () { var h = horizontalSensitivity Input.GetAxis ("Mouse X"); var v = verticalSensitivity Input.GetAxis ("Mouse Y"); transform.Rotate (-v,-h,0); outOfLock(); }

function outOfLock () { if (Input.GetKeyDown(KeyCode.Escape)) { Cursor.lockState = CursorLockMode.None; } }

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
0

Answer by Robert_s_a · Mar 12, 2017 at 02:03 AM

@crazygamerdl1 Hi,

The problem you're encountering is because you are rotating around two axes, and once you have rotated even a little bit, the axes are now tilted, so subsequent rotations will tilt the horizon. This is much more apparent if you have something to look at. If you're interested in a visual of what is happening, you can create three cylinders called Xaxis, Yaxis, and Zaxis, scale them all to (0.1, 2, 0.1), and then rotate them in the Inspector so that they are pointing along each axis. (No rotation on Yaxis, (0, 0, -90) on Xaxis, and (90, 0, 0) on Zaxis) then translate them all to be at the same location as your camera. From there, in the hierarchy, attach them as children to the camera. From here, you can run your script and watch your camera tilt and turn in the Scene window. You'll see how combinations of rotations in X and Y will result in the whole thing turning and twisting, because you are rotating around axes that are tilted.

If you're not interested, you can just proceed to the script below which will do what I think you are expecting. I did this using Quaternions and keeping a record of the total rotation you have input. Then I actually set the value of transform.rotation based on this quaternion. You can't plug numbers directly in this way without the quaternion .Euler transformation because quaternions aren't really numbers that humans use. LOL Anyway, basically I keep track of the total rotation you input, and send that in to your camera as if it were one rotation from (0,0,0) to wherever you have rotated to in total, rather than doing it in step by step increments which proceed from the last rotation. (I hope I'm not confusing you with this explanation as much as I'm confusing myself trying to type it) Essentially, you can spin left/right and up/down all you want, and the horizon will stay horizontal.

Enough babble - on to the code. I'm in C#, so you'll have to tweak the syntax to get it into Java:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class TestCamera : MonoBehaviour {

     private float horizontalSensitivity = 10.0f;
     private float verticalSensitivity = 10.0f;
     private float v;
     private float h;
     private float totalV;
     private float totalH;
     private Quaternion tempRotation;
 
 void Start ()
     {
         Cursor.lockState = CursorLockMode.Locked;
     }
 
 void Update ()
     {
         // Read user input
         h = horizontalSensitivity * Input.GetAxis("Mouse X");
         v = verticalSensitivity * Input.GetAxis("Mouse Y");

         // Accumulate total rotation from all input to date
         totalV = totalV -= v;
         totalH = totalH -= h;

         // Calculate the single Quaternion rotation necessary to get us here
         tempRotation = Quaternion.Euler(totalV, totalH, 0.0f);

         // Apply that single rotation to the camera (from the origin)
         transform.rotation = tempRotation;
         outOfLock();
     }

     private void outOfLock()
     {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             Cursor.lockState = CursorLockMode.None;
         }
     }
 }

Hope this helps. :)

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 crazygamerdl1 · Mar 12, 2017 at 03:21 AM 0
Share

I tested your code and, it had the same problem I did. It would tilt although it was not as bad as it was in my code.

avatar image Robert_s_a crazygamerdl1 · Mar 12, 2017 at 03:39 AM 0
Share

I did notice one error in my code above - doesn't affect things in C#, it's just redundant, but it may act differently in Java. The lines totalV = totalV -= v; (and the same for h) should just be totalV -= v; (or h). I can't see that causing problems with tilt though. On my machine, I can move the mouse all over, in any direction and the horizon stays horizontal without any tilt. I can tilt up, all the way around until I'm upside down, and continue until I'm right side up again, at any angle left or right, and the horizon stays flat. I'm not sure what else to suggest. Anyone else with input?

avatar image Robert_s_a crazygamerdl1 · Mar 12, 2017 at 03:58 AM 0
Share

Is there anything else which affects the orientation of your camera? If so, that will need to be looked at as well, because that could be causing a tilt. That's the only other thing that I can think of at this point.

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

129 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 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 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 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 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

Rotation smoothing 0 Answers

Rotate towards mouse pointer 1 Answer

TopDown Look at mouse 0 Answers

How Can I Convert Vector3 From Local To Global(World)? 0 Answers

Object Rotate with Camera 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