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 /
avatar image
0
Question by jdurantt0 · Nov 01, 2015 at 01:30 PM · c#camerarotation

Limit Rotation around X Axis?

I'm completely lost, me and a group of friends who are relatively new to this are trying to create a custom mouse look script and have all but the clamping of the axis done. We have no knowledge of euler angles and quaternions and I really can't find anything that makes sense so was hoping I could get some help here, basically we want to limit the rotation of the camera on the X axis, I understand the if and else if statements are incorrect but we couldn't find any way to do it that makes sense, we have knowledge of Mathf.Clamp () but we can't figure out how to use it.

     void Awake () {
         Player = GameObject.Find ("Player");
     }
 
     void Update () {
         float mouseX = Input.GetAxis ("Mouse X") * Xsensitivity;
         float mouseY = Input.GetAxis ("Mouse Y") * Ysensitivity;
 
         mouseY = -mouseY;
 
         Cursor.lockState = CursorLockMode.Locked;
 
         if (mouseY > 0 && mouseY < 60) {
             mouseY = mouseY;
         } else if (mouseY >= 60 && mouseY <= 180) {
             mouseY = 60;
         } else if (mouseY <= 300 && mouseY >= 180) {
             mouseY = 300;
         } else if (mouseY >= 300 && mouseY <= 360) {
             mouseY = mouseY;
         }
         transform.Rotate (mouseY, 0, 0);
         Player.transform.Rotate (0, mouseX, 0);
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Angelord · Nov 01, 2015 at 11:25 PM

Hi! So i cleaned up the code a bit (not sure why you were locking the cursor). I only left what is relative to the clamping :

   using UnityEngine;
     using System.Collections;
     
     public class MouseLook : MonoBehaviour {
         public float Ysensitivity;
         private float rotationY = 0f;
         void Update () {
             rotationY += Input.GetAxis ("Mouse Y") * Ysensitivity;
     
     
             rotationY = Mathf.Clamp (rotationY, -15, 15);
             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, transform.localEulerAngles.z);
         }
     }

As you can see you use Mathf.Clamp by giving it the variable you want to clamp and then the minimum and maximum values between which you want to clamp it. Then i simply assign the value to the transform's local euler angles. I also store the current rotation in a variable at the start and modify it using the values from Input.GetAxis. Just go ahead and replace -15 and 15 with your own boundries and everything should work just fine.

Comment
Add comment · Show 4 · 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 jdurantt0 · Nov 02, 2015 at 01:32 PM 0
Share

This looks really interesting, haven't had time to try it but when I do I'll let you know how it works!

avatar image jdurantt0 · Nov 02, 2015 at 03:11 PM 0
Share

O$$anonymous$$G I LOVE YOU THAN$$anonymous$$ YOU SO $$anonymous$$UCH!!!! I've been stuck on this for several days!!

avatar image nreed132 · Feb 10, 2020 at 04:39 AM 0
Share

I've been searching all day for something like this and almost just gave up on the project. While I don't know quite why nothing I did worked, I thank you greatly for providing this to the world!

avatar image OGNE · Apr 05, 2020 at 02:05 PM 0
Share

Works like a CHAR$$anonymous$$ for me..Thanks.

avatar image
1

Answer by ElDo · Nov 01, 2015 at 09:07 PM

I also wrote my own MouseLook script and faced the same Problem you're now facing. You didn't mention how your Camera Object is made so I'll explain how I solved it. My Controllable GameObject is in the Scene (no parents) as a Child Object to that one I have an Empty GameObject I called XRot which is only meant for the Rotation of my Camera around the X-Axis and my Camera Object self is a Child of XRot. Therefor I can set my Camera at any distance I want with it's local Position and Rotate around some other Point (the XRot Object) around the X-Axis. MY Script now has a Transform reference to the XRot Object and the code Looks like this:

         //Camera Stuff (rotating Character/looking around)
         Vector3 rot = xRot.localRotation.eulerAngles;
         rot.x -= Input.GetAxis(PlayerInput.MouseY) * 50 * camSpeed * Time.deltaTime;
         if (rot.x > 180) rot.x -= 360;
         rot.x = Mathf.Clamp(rot.x, -60, 60);
         xRot.localRotation = Quaternion.Euler(rot);
         transform.Rotate(0, Input.GetAxis(PlayerInput.MouseX) * 50 * _unit.moveSpeed * Time.deltaTime, 0);

About Mathf.Clamp it works pretty simple as soon as you understand the the return value is the clamped value you can see how I used it in my code. here I Limit the X-Rotation from -60° to 60° relative to the initial Position 0° I used Time.deltaTime because that code snippet is inside the Update() Event. Hope this helps.

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
avatar image
0

Answer by the_genius · Nov 01, 2015 at 05:55 PM

Rather than trying to reinvent the wheel there is the MouseLook.cs script in the Characters Unity Standard Assets package part of the standard First Person character prefab.

I've taken a look and it seems to have maximum and minimum rotations on the x axis.

You could use that instead or atleast take a look at how they did it.

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 jdurantt0 · Nov 01, 2015 at 06:00 PM 0
Share

I took a look at the script and it's a bunch of quaternions and euler angles that i have no idea how they work, I was also told that I shouldn't use the mouselook script and ins$$anonymous$$d should create my own because it's poorly designed.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Camera to follow the player in the form of radius 1 Answer

Can't do Quaternion.AngleAxis twice. 1 Answer

How to make Camera position Independent of its Rotation? 1 Answer

Smooth Follow Camera Rotate on Z-Axis? 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