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 charlottie333 · Apr 21, 2016 at 11:05 PM · c#camera rotatemouselookresetrestrictions

Restrict camera rotation

I've found this script that works perfectly for how I want the camera to move. I want to restrict it so you can't look all the way around. I've looked at mathf and clamp functions, but being a total noob, I mess up the whole thing whenever I try to do anything.

 using UnityEngine;
 using System.Collections;
 
 public class MouseView : MonoBehaviour 
 {
     public float Sensitivity = 0.125f;
 
     Vector3 _prevMousePosition = Vector3.zero;
     float   _tilt = 0;
     float   _turn = 0;
 
     void Update () 
     {       
         Vector3 mousePosition = Input.mousePosition;
         if( Input.GetMouseButton( 0 ) )
         {
             _tilt += -( mousePosition.y-_prevMousePosition.y ) * Sensitivity;
             _turn += ( mousePosition.x-_prevMousePosition.x ) * Sensitivity;
         }
         _prevMousePosition = mousePosition;
         transform.localRotation = Quaternion.Euler( _tilt, _turn, 0 );
     }
 }

Also, I want to reset the view/script when "v" is pressed.

Any advice would be much 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
1
Best Answer

Answer by fabian-mkv · Apr 22, 2016 at 12:09 AM

First, define the range with these 4 variables:

 float minTiltAngle = -45f;
 float maxTiltAngle = 90f;
 float minTurnAngle = -60f;
 float maxTurnAngle = 60f;

Then, Immediately after setting _tilt and _turn, you could clamp them like so:

 _tilt += Mathf.Clamp(_turn, minTiltAngle, maxTiltAngle);
 _turn = Mathf.Clamp(_turn, minTurnAngle, maxTurnAngle);

For your "v to reset" problem, try putting this in the Update function:

 if (Input.GetKeyDown(KeyCode.V)) {
     tilt = 0f;
     turn = 0f;
 }

Update gets called every frame. when the player presses V for the first time, GetKeyDown for the key V will return true. If the player keeps holding it, this function will not be true for subsequent frames. If you want to check every frame whether a key is being held down, use Input.GetKey

All put together: using UnityEngine; using System.Collections;

  public class MouseView : MonoBehaviour 
  {
      public float Sensitivity = 0.125f;
  
      Vector3 _prevMousePosition = Vector3.zero;
      float   _tilt = 0;
      float   _turn = 0;
      float minTiltAngle = -45f;
      float maxTiltAngle = 90f;
      float minTurnAngle = -60f;
      float maxTurnAngle = 60f;
      
      void Update () 
      {       
          Vector3 mousePosition = Input.mousePosition;
          if( Input.GetMouseButton( 0 ) )
          {
              _tilt += -( mousePosition.y-_prevMousePosition.y ) * Sensitivity;
              _turn += ( mousePosition.x-_prevMousePosition.x ) * Sensitivity;
              _tilt += Mathf.Clamp(_turn, minTiltAngle, maxTiltAngle);
              _turn = Mathf.Clamp(_turn, minTurnAngle, maxTurnAngle);             }
              _prevMousePosition = mousePosition;
              transform.localRotation = Quaternion.Euler( _tilt, _turn, 0 );
      }
      if (Input.GetKeyDown(KeyCode.V)) {
          tilt = 0f;
          turn = 0f;
      }
  }






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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Help My camera rotation script when a player hits a wall or game object it goes crazy. How can i change it to prevent this. 0 Answers

Prevent Reset() from clearing out serialized fields 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