• 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
Question by RealMTG · Jun 20, 2015 at 09:05 PM · rotationvector3float

How can I replace this Vector3 with a float?

Hi!

I am following a tutorial to get my player controller working. I finished the tutorial and started to add on things. When I got the camera rotation I needed to add some extra variables but that messes up the camera rotation. At around 14:26 in the video he makes the camera rotate by using 'cameraT.localEulerAngles = Vector3.left * verticalLookRotation;', and that works good. But not good enough for me. So firstly I tried to replace it with:

 cameraHead.localEulerAngles = new Vector3(Vector3.left * verticalLook, cameraHead.rotation.y, inputSensitivity * Time.deltaTime);

But of course, that won't work since you can't seem to have a Vector3 in a new Vector3. So I later tried to remove the Vector3.left and keep the vertical look. That worked fine except the vertical look was inverted. So I later tried this:

 cameraHead.localEulerAngles = new Vector3(Input.GetAxis("Mouse X") * verticalLook, cameraHead.rotation.y, inputSensitivity * Time.deltaTime);

Now that breaks the whole camera. It keeps rotating but at the same time trying to get back to the value of 0.

So basically, what can I do to get it working as I want it to work? Be able to look up and down and not it being inverted.

Thanks in advance!

If any further questions about my code, please ask. I did my best to explain all you need.

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by AurimasBlazulionis · Jun 20, 2015 at 09:24 PM

Where is verticalLook being declared? If it is some float, just use it without any Input.GetAxis or Vector3.left, because you are already setting value of x axis

Comment
RealMTG

People who like this

1 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 RealMTG · Jun 20, 2015 at 09:27 PM 0
Share

Oh sorry. I missed to include that.

This is the code that declares the vertical look (float).

             verticalLook += Input.GetAxis("Mouse Y") * Time.deltaTime * mouseSettings.mouseSensitivityY;
             verticalLook = Mathf.Clamp(verticalLook, mouseSettings.clampMinimumX, mouseSettings.clampMaximumX);

It is being declared right before the code from above.

I also tried to remove the "Input.GetAxis" and that's what is making the input inverted.

avatar image AurimasBlazulionis · Jun 20, 2015 at 09:29 PM 1
Share

if it get's inverted, try using verticalLook -= blablabla, instead of +=

avatar image RealMTG · Jun 20, 2015 at 09:32 PM 0
Share

@TheDiamondPlay Thanks. I can't believe that I didn't think about that...

avatar image

Answer by KevinCodes4Food · Jun 20, 2015 at 09:33 PM

Hi RealMTG,

If I understand what you are trying to accomplish, you would like to have the mouse X axis input map to vertical movement on the camera.

The first key issue is that Input.GetAxis("Mouse X") only returns delta move values, and zero when the mouse is not moving. Assigning this directly to X rotation is why your camera rotation keeps going back to zero.

You'll need to store the mouse X movement in an accumulator that retains its value across Update() calls to add to or remove from the mouse look each frame. Probably something like this:

     float mouseAccumulator = 0;
     void Update () {
 
         ...
 
         mouseAccumulator += Input.GetAxis("Mouse X") * inputSensitivity;
         mouseAccumulator = Mathf.Clamp(mouseAccumulator, -89, 89);
 
         float x = mouseAccumulator;
         float y = cameraHead.transform.eulerAngles.y;
         float z = inputSensitivity * Time.deltaTime;
 
         cameraHead.transform.localEulerAngles = new Vector3(x,y,z);
             ...
 
     
     }

Second issue is that typically the local Euler angle vector is treated as an X, Y, Z rotation. Rotation on X is looking up/down, Y left/right, and Z rotating like a barrel roll. Your code is using mouse X, left right, to go up/down, which is odd. Also, your formula is setting Z to a not quite zero value each frame, which is odd, too. So I am guessing that what you really would want to do something like this:

         Vector3 lookDirectionVector = Vector3.zero;
     void Update () {
         ...
 
         lookDirectionVector.x += Input.GetAxis("Mouse Y") * inputSensitivity;  // Up & Down
         lookDirectionVector.y += Input.GetAxis("Mouse X") * inputSensitivity; // Left & Right
         lookDirectionVector.z = 0; // Roll

         lookDirectionVector.x = Mathf.Clamp(lookDirectionVector.x, -89, 89);
 
         cameraHead.transform.localEulerAngles = lookDirectionVector;
         ...
     }

  

Finally, if you need to invert up/down movement, just change the X vector to -= instead of +=.

Comment

People who like this

0 Show 0 · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Accuracy issues with Float (Vector3) 2 Answers

Character Controller Movement - Different speeds on different axis 1 Answer

Limit distance from center 0 Answers

Sprite Rotation Messed up,Rotation is choppy and messed up 0 Answers

Perpendicular Vector3 0 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