• 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 Heatmanofurioso · Mar 03, 2016 at 08:04 PM · mathf.clamp

Mathf Clamp not working as expected

Hi, I have an issue on my code, in a 3D 1st Person Controller i am trying to develop. I am attempting to clamp my y value on my camera rotation in order to make it behave correctly. But, for some reason, the clamp is not working.

Player Controller:

 using UnityEngine;
 
 [RequireComponent(typeof(PlayerMotor))]
 public class PlayerController : MonoBehaviour {
 
     //Camera Rotation Max and Min value
     [SerializeField]
     private float MaxMinCameraY = 60f;
     //Vertical Y Camera Axis Rotation Value
     public float yRot;
     //Horizontal X Camera Axis Rotation Value
     public float xRot;
     //Horizontal X Movement Value
     public float xMov;
     //Forwarz Z Movement Value
     public float zMov;
     //Jump Power
     [SerializeField]
     private float JumpPower = 5f;
     //Character Running
     [SerializeField]
     private bool isRunning = false;
     //Character Crouching
     [SerializeField]
     private bool isCrouching = false;
     //Speed being used
     public static float setSpeed;
     //Walking Speed
     [SerializeField]
     private float walkingSpeed = 10.0f;
     //Running Speed
     [SerializeField]
     private float runningSpeed = 20.0f;
     //Crouching Speed
     [SerializeField]
     private float crouchingSpeed = 6.0f;
     //Look Sensitivity
     [SerializeField]
     private float lookSensitivity = 3f;
     //Character touching the ground
     [SerializeField]
     private bool onGround = true;
     //RigidBody
     private Rigidbody rb;
     //Player Motor
     private PlayerMotor motor;
 
     void Start ()
     {
         motor = GetComponent<PlayerMotor>();
         rb = GetComponent<Rigidbody> ();
         setSpeed = walkingSpeed;
         onGround = true;
     }
 
     void Update ()
     {
         //Axis Variables
         xMov = Input.GetAxisRaw("Horizontal");
         zMov = Input.GetAxisRaw("Vertical");
 
         //Detect if character is running, walking or crouching, to set speed
         if (Input.GetKeyDown("left shift") && !isCrouching)
         {
             isRunning = true;
             setSpeed = runningSpeed;
         }
         if (Input.GetKeyUp("left shift"))
         {
             isRunning = false;
             setSpeed = walkingSpeed;
         }
         if (Input.GetKeyDown("c") || !isRunning)
         {
             isCrouching = true;
             setSpeed = crouchingSpeed;
         }
         if (Input.GetKeyUp("c"))
         {
             isCrouching = false;
             setSpeed = walkingSpeed;
         }
 
         //Set desired speed. According to Running, Crouching or Walking
         float translation = Input.GetAxis ("Vertical") * setSpeed;
         translation *= Time.deltaTime;
 
         //Vector3s for movement
         Vector3 _movHorizontal = transform.right * xMov;
         Vector3 _movVertical = transform.forward * zMov;
 
         // Final movement vector
         Vector3 _velocity = (_movHorizontal + _movVertical).normalized * setSpeed;
 
         //Apply movement
         motor.Move(_velocity);
 
         //Horizontal Rotation
         //Calculate rotation as a 3D vector (turning around)
         xRot = Input.GetAxisRaw("Mouse X");
         //Make it turn around the Y axis. And add the look sensitivity to it
         Vector3 _rotation = new Vector3(0f, xRot, 0f) * lookSensitivity;
         //Apply rotation
         motor.Rotate(_rotation);
 
         //Calculate camera rotation as a 3D vector (turning up and down)
         yRot = Input.GetAxisRaw("Mouse Y") * lookSensitivity;
 
 
         yRot = Mathf.Clamp (yRot, -MaxMinCameraY, MaxMinCameraY);
 
         Vector3 cameraRotation = new Vector3(yRot, 0f, 0f);
 
         //Apply camera rotation
         motor.RotateCamera(cameraRotation);
 
         //If character jumps
         if (Input.GetKeyDown("space") && onGround)
         {
             rb.velocity = new Vector3 (0f, JumpPower, 0f);
             onGround = false;
         }
         //If character jumps
         if (Input.GetKeyUp("space") && !onGround)
         {
             onGround = true;
         }
     }
 }
 

Player Motor:

 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class PlayerMotor : MonoBehaviour {
 
     //Player Camera
     [SerializeField]
     private Camera cam;
     //Movement Velocity Vector3
     private Vector3 velocity = Vector3.zero;
     //Rotation Vector3
     private Vector3 rotation = Vector3.zero;
     //Camera Rotation Vector3
     private Vector3 cameraRotation = Vector3.zero;
     //RigidBody
     private Rigidbody rb;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     // Gets a movement vector
     public void Move (Vector3 _velocity)
     {
         velocity = _velocity;
     }
 
     // Gets a rotational vector
     public void Rotate(Vector3 _rotation)
     {
         rotation = _rotation;
     }
 
     // Gets a rotational vector for the camera
     public void RotateCamera(Vector3 _cameraRotation)
     {
         cameraRotation = _cameraRotation;
     }
 
     // Run every physics iteration
     void FixedUpdate ()
     {
         PerformMovement();
         PerformRotation();
     }
 
     //Perform movement based on velocity variable
     void PerformMovement ()
     {
         if (velocity != Vector3.zero)
         {
             rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
         }
     }
 
     //Perform rotation of the camera with the body
     void PerformRotation ()
     {
         rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
         if (cam != null)
         {
             cam.transform.Rotate(-cameraRotation);
         }
     }
 
 }
 
Comment
Add comment · Show 1
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 Graphics_Dev · Mar 03, 2016 at 08:29 PM 0
Share

What do you mean that its not working?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Magius96 · Mar 03, 2016 at 09:28 PM

We don't really know what you mean when you say it's not working when you don't tell us what it is doing. But looking at your code, I have to question this:

 yRot = Mathf.Clamp (yRot, -MaxMinCameraY, MaxMinCameraY);
 Vector3 cameraRotation = new Vector3(yRot, 0f, 0f);

Why are you applying your yRot to the X value of your camera rotation? I also see that you are applying your xRot to the y value of the rotation. Was this intentional?

Comment
Add comment · Show 1 · 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 Heatmanofurioso · Mar 03, 2016 at 10:12 PM 0
Share

It was a misspelling in my values. I actually fixed the issue. I completely removed the $$anonymous$$otor, and simplified the code, having only a controller, and reusing variables to clean the code a bit.

The issue was that the clamp was simply not working. But, i managed to do so finally with the help also of Quaternion Eulers :)

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

49 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

Related Questions

Random.Range producing values outside its range. 1 Answer

Help with clamping rotation, and using animations 0 Answers

Please help me! Correct script) , 0 Answers

Space Shooter tutorial - Mathf.clamping boundaries 0 Answers

Mathf.clamp cancels my trigger 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