Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
avatar image
2
Question by undead504 · Jun 01, 2016 at 02:43 PM · javascriptmovementtransformmouselook

How can i Limit the Mouse X Axis for Mouse Look

Hello there I need to Limit the Players Rotation on the X Axis. I can Move the mouse and it will rotate the Player but i need to Stop about half way.

Here is what i got so far.

var VerticalSpeed : float = 2.0; var HorizontalSpeed : float = 2.0; var PlayerObject : Transform;

function Update (){ Screen.lockCursor=true; Cursor.visible=true; Screen.lockCursor=false; var h : float = HorizontalSpeed *Input.GetAxis("Mouse X"); var v : float = VerticalSpeed *Input.GetAxis("Mouse Y"); transform.Rotate(-v,0,0); PlayerObject.Rotate(0,h,0); }

Comment
Add comment · Show 3
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 Raresh · Jun 01, 2016 at 03:29 PM 0
Share

First of all, you say Limit the mouse Y axis, then you say limit the rotation on the X axis. Can you be more clear on which axis you need? What do you mean by "half way"? It can mean a lot of things. You want it to only rotate 180 degrees? 180 degrees to the middle of the player?

Be more specific.

avatar image undead504 · Jun 01, 2016 at 06:05 PM 0
Share

Sorry for any Confusion But see i Put y in the Title I meant X not Y my bad. changed it.

I don't want the Player to Be able to look all the Way around just Half way on both sides.

avatar image Nikhil11 · Oct 06, 2017 at 09:34 AM 0
Share

Thanks This helped me lot

2 Replies

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

Answer by Soos621 · Jun 01, 2016 at 03:39 PM

 public float sensitivityX = 15F;
 public float sensitivityY = 15F;

 public float minimumX = -90F;
 public float maximumX = 90F;

 public float minimumY = -60F;
 public float maximumY = 60F;

 float rotationY = 0F;
 float rotationX = 0f;

 void Update (){
     rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
     rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);

     rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

     transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
 }

the mathf.clamp will limit the angle of the object to the min or maximum degrees

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 undead504 · Jun 01, 2016 at 03:49 PM 0
Share

How can i Convert this to JavaScript

avatar image Soos621 undead504 · Jun 01, 2016 at 06:40 PM 0
Share

you will have to convert it manually, i just pulled this from one of my projects but i only code in c# i wouldnt be able to give you an accurate translation, but i know that its not hard, i have had to decipher from js to c#

avatar image Igorotak · Mar 27, 2018 at 09:24 PM 0
Share

I tried doing this and combining it with my movement script but it wont work. The camera always reverts back to 0 after Update()

avatar image fauly Igorotak · Feb 14, 2020 at 04:22 PM 0
Share

$$anonymous$$ake sure you're setting the currrent rotation in either Start() direct in monobehaviors - not in your movement function probably called every Update().

avatar image
1

Answer by Mynameisbec · Jan 26, 2021 at 06:17 AM

 public float sensitivityX = 15F;
  public float sensitivityY = 15F;
  public float minimumX = -90F;
  public float maximumX = 90F;
  public float minimumY = -60F;
  public float maximumY = 60F;
  float rotationY = 0F;
  float rotationX = 0f;
  void Update (){
      rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
      rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
      rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
      rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
      transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
  }
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

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

Problem using rotation and Rotate 1 Answer

Unity's mouse look causing movement issues 1 Answer

Best Way to make a character move 1 Answer

Triggering movement from touching gameObject (Javascript issue) 1 Answer

Simplest movement to point possible 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