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 SlimJimmi · Sep 06, 2016 at 08:49 AM · c#mouselookx-axisclamped rotationwontwork

C# Mouse Look Script | Why does the Y Axis Work but not the X Axis?

Hello!

I've been teaching myself the basics of Unity lately by creating a first-person game.

Everything works except for my C# mouse look code. The camera rotates up and down on a clamped Y axis (as desired), however, the camera won't rotate left or right on the X-axis.

However, if I remove this code from the mouse look script:

mouseUpDown = Mathf.Clamp(mouseUpDown, -mouseLookLimit, mouseLookLimit); Camera.main.transform.localRotation = Quaternion.Euler(mouseUpDown, 0, 0);

The camera can rotate on the X-axis again.

I've posted my code below along with a screenshot of my inspector panel/character controller; can anyone explain what's gone wrong and how to fix it?

If you need anymore information or screenshots, let me know and I'll post it.

Thanks in advance!

Andy


C# Script:

 using UnityEngine;
 using System.Collections;
 
 public class playerMovement : MonoBehaviour
 {
 
     public float moveSpeed, walkSpeed, runSpeed, crouchSpeed; 
     public float mouseSensitivity, mouseUpDown, mouseLeftRight, mouseLookLimit; 
     public float verticalJump, jumpStrength, jumpGravity;
     CharacterController crouchMove; 
     CharacterController jumpMove;
 
 
     void Start()
     {
         crouchMove = gameObject.GetComponent<CharacterController>(); 
         jumpMove = gameObject.GetComponent<CharacterController>();
     }
 
 
     void Update()
     {
 
 //------------------- Forwards, Backwards, Strafe Side-to-Side Movement -------
 
         float forwardBackMove = Input.GetAxis("Vertical") * moveSpeed; 
         float leftRightMove = Input.GetAxis("Horizontal") * moveSpeed; 
         Vector3 speed = new Vector3(leftRightMove, 0, forwardBackMove); 
         speed = transform.rotation * speed;
 
 //------------------- Sprinting  --------------------------------------------------------------------
 
         if (Input.GetKey(KeyCode.LeftShift)) 
         {
             moveSpeed += 5;
 
             if (moveSpeed >= runSpeed)
             {
                 moveSpeed = runSpeed;
             }
         }
         else
         {
             moveSpeed = walkSpeed;
         }
 
 //------------------- MOUSE LOOK SCRIPT -----------------------------------------------
 
         mouseLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity; 
         transform.Rotate(0, mouseLeftRight, 0); 
 
         mouseUpDown -= Input.GetAxis ("Mouse Y") * mouseSensitivity; 
         mouseUpDown = Mathf.Clamp(mouseUpDown, -mouseLookLimit, mouseLookLimit); 
         Camera.main.transform.localRotation = Quaternion.Euler(mouseUpDown, 0, 0); 
 
 
 //------------------- CROUCHING AND STANDING -----------------------------------
 
         if (Input.GetKey(KeyCode.LeftControl)) 
         {
             crouchMove.height = 1.0f; 
             moveSpeed = crouchSpeed;
         }
         else 
         {
             crouchMove.height += 0.3f; 
             if (crouchMove.height >= 1.96) { 
                 crouchMove.height = 1.96f;
             }
 
         }
 
 //------------------- JUMPING AND GRAVITY------------------------------------------------
 
         if (jumpMove.isGrounded) {
             verticalJump = -jumpGravity * Time.deltaTime;
             if (Input.GetKey(KeyCode.Space)){
                 verticalJump = jumpStrength;
             }
         }
         else
         {
             verticalJump -= jumpGravity * Time.deltaTime;
         }
         Vector3 jumpVector = new Vector3(0, verticalJump, 0);
         jumpMove.Move(jumpVector * Time.deltaTime);
 
 
 //------------------- CHARACTER CONTROLLER ----------------------------------------
 
         CharacterController  playerController = GetComponent<CharacterController>(); 
         playerController.SimpleMove(speed); 
         
     }
 }


alt text

inspector.jpg (65.9 kB)
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 metalted · Sep 06, 2016 at 09:25 PM

You have kind of figured it out yourself... almost!

 mouseUpDown = Mathf.Clamp(mouseUpDown, -mouseLookLimit, mouseLookLimit); Camera.main.transform.localRotation = Quaternion.Euler(mouseUpDown, 0, 0);

You are setting the eulerAngles of the camera, no problem with that, but you are setting it to a Vector3(mouseUpDown, 0, 0); The eulerAngles of the camera in the y and z axis will always be zero; This may be the cause of the camera not rotating. Make sure that setting one rotation will not mess up the other rotation.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

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

Clamping camera rotation 1 Answer

how do i make my immovable 'empty gameobject spawnpoint' shoot bullets in the direction of my crosshair? 0 Answers

[C#]detect object on mouse look? 1 Answer

How To Make My Player Only Move on the ground? 1 Answer

How to Clamp a 3rd Person Camera's x-axis Rotation? 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