Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 cmcmains · Mar 17, 2017 at 07:56 PM · camerarotationmathmathf.clamp

How can I use Mathf.Clamp?

I've got a basic movement script going but I can't figure out how to use Mathf.Clamp so that the player can't do flips with the camera.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 [RequireComponent (typeof(CharacterController))]
 public class PlayerMove : MonoBehaviour {
 
     public float playerSpeed = 2f;
     public float mouseSens = 5f;
 
     [SerializeField]
     private CharacterController controller;
 
     [SerializeField]
     private Camera cam;
 
     private float moveVertical;
     private float moveHorizontal;
 
     private float rotationY;
     private float rotationX;
 
     private float maxCameraRotation = 10f;
 
 
     void Start ()
     {
 
         controller = GetComponent <CharacterController> ();
 
     }
 
 
     void Update () 
     {
 
 
         moveVertical = Input.GetAxis ("Vertical") * playerSpeed;
         moveHorizontal = Input.GetAxis ("Horizontal") * playerSpeed;
 
         rotationY = Input.GetAxis ("Mouse X") * mouseSens;
         rotationX = Input.GetAxis ("Mouse Y") * mouseSens;
 
         Vector3 movement = new Vector3 (moveHorizontal, 0f, moveVertical);
 
         transform.Rotate (0f, rotationY, 0f);
         cam.transform.Rotate (-Mathf.Clamp (rotationX, -maxCameraRotation, maxCameraRotation), 0f, 0f);
 
         movement = transform.rotation * movement;
         controller.Move (movement * Time.deltaTime);
 
     }
 }


That's the whole script, including my current attempt. Help would be appreciated!

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 Namey5 · Mar 18, 2017 at 01:54 AM

In this case, don't use transform.Rotate. The value used in this function is simply the direction in which to turn the object, so clamping that simply limits how fast the object can turn. Instead, you have to do the x rotation manually.

 float rotationX = 0.0f;
 
 void Update ()
 {
     rotationX += mouseSens * Input.GetAxis ("Mouse Y") * Time.deltaTime;
     rotationX = Mathf.Clamp (rotationX, -maxCameraRotation, maxCameraRotation);
     
     cam.transform.eulerAngles = new Vector3(rotationX, transform.eulerAngles.y, transform.eulerAngles.z);
 }
Comment
Add comment · Show 2 · 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 cmcmains · Mar 18, 2017 at 02:56 PM 0
Share

It's saying that I can't modify that value type when I do what you've shown. Any ideas?

avatar image Namey5 cmcmains · Mar 18, 2017 at 09:48 PM 0
Share

Sorry, I wrote the last line wrong. Forgot that rotations in Unity are Quaternions, not Eulers. Answer has been edited.

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

How to Properly Mathf.Clamp an X Rotation for Camera? 0 Answers

Rotating a camera around a game object? 2 Answers

Camera rotate X degree based on player rotate 0 Answers

Camera Rotation Switching Problem 0 Answers

Creating a camera controller for isometric view - Positioning the camera 1 Answer


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