• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Understanding The Mouselook C# Script 1 Answer

Doing stuff once a day 2 Answers

How could i reset those objects? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges