Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 deirre · May 07, 2013 at 08:15 PM · camerarotationmouserotate

Limiting the rotation to 180 degrees

I need assistance limiting the rotation to 180 degrees. Basically the script rotates 360 degrees around an object with smoothing and occlusion handling. Any assistance is helpful.

using UnityEngine; using System.Collections;

public class TP_Camera : MonoBehaviour { public Texture2D textureToDisplay;

 public static TP_Camera Instance;
 public Transform TargetLookAt;
 
 public float Distance = .5f;
 public float DistanceMin = .2f;
 public float DistanceMax = 1f;
 public float DistanceSmooth = 0.1f;
 
 //public float X_Smooth = 0.05f;
 //public float Y_Smooth = 0.1f;
 public float X_Smooth = .005f;
 public float Y_Smooth = .01f;

 public float X_mouseSensitivity = .5f;
 public float Y_mouseSensitivity = .5f;
 public float mouseWheelSensitivity = 5f;

 public float y_MinLimit = -10f;
 public float y_MaxLimit =  90f;

 public float OccLusionDistanceStep = 0.05f;
 public int MaxOcclusionCheck = 1;

 private float mouseX = 0f;
 private float mouseY = 0f;
 
 private float startDistance = 0f;
 private float desiredDistance = 0f;
 //private float desiredPosition = 0f;

 private float velX = 0f;
 private float velY = 0f;
 private float velZ = 0f;
 private float velDistance = 0f;

 private Vector3 position = Vector3.zero;
 private Vector3 desiredPosition = Vector3.zero;    

 void awake()
 {
     Instance = this;
 }
 
 void Start () 
 {
     Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax);
     startDistance = Distance;
     reset();
 }
 
 
 void LateUpdate () 
 {
     if (TargetLookAt == null)
         return;
     
     HandlePlayerInput();

     var count = 0;

     do
     {
         CalculateDesiredPosition();
         count++;
     } while (CheckIfOccluded(count));        

     //CheckCameraPoints(TargetLookAt.position, desiredPosition);

     UpdatePosition();

 }

 Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
 {
     Vector3 direction = new Vector3(0, 0, -distance);
     Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
     return TargetLookAt.position + rotation * direction;
 }

 bool CheckIfOccluded(int count)
 {
     var isOccluded = false;

     var nearestDistance = CheckCameraPoints(TargetLookAt.position, desiredPosition);

     if(nearestDistance != -1)
     {
         if (count < MaxOcclusionCheck)
         {
             isOccluded = true;
             Distance -= OccLusionDistanceStep;

             //if (Distance < 0.25f)
             //    Distance = 0.25f;
             if (Distance < 0.0025f)
                 Distance = 0.0025f;
         }
         else           
             Distance = nearestDistance - Camera.mainCamera.nearClipPlane;

         desiredDistance = Distance;
     }

     return isOccluded;
 }

 float CheckCameraPoints(Vector3 from, Vector3 to)
 {
     var nearestDistance = -1f;

     RaycastHit hitInfo;

     Helper.ClipPlanePoints clipPlanePoints = Helper.ClipPlaneAtNear(to);

     //Draw lines for test/debug in editor
     Debug.DrawLine(from, to + transform.forward * -camera.nearClipPlane, Color.red);
     Debug.DrawLine(from, clipPlanePoints.UpperLeft);
     Debug.DrawLine(from, clipPlanePoints.LowerLeft);
     Debug.DrawLine(from, clipPlanePoints.UpperRight);
     Debug.DrawLine(from, clipPlanePoints.LowerLeft);
     //Draw pyramid
     Debug.DrawLine(clipPlanePoints.UpperLeft, clipPlanePoints.UpperRight);
     Debug.DrawLine(clipPlanePoints.UpperRight, clipPlanePoints.LowerRight);
     Debug.DrawLine(clipPlanePoints.LowerRight, clipPlanePoints.LowerLeft);
     Debug.DrawLine(clipPlanePoints.LowerLeft, clipPlanePoints.UpperLeft);

     //Line casting to determine nearest points
     if (Physics.Linecast(from, clipPlanePoints.UpperLeft, out  hitInfo) && hitInfo.collider.tag != "Player")
         nearestDistance = hitInfo.distance;

     if (Physics.Linecast(from, clipPlanePoints.UpperLeft, out hitInfo) && hitInfo.collider.tag != "Player")
         if(hitInfo.distance < nearestDistance || nearestDistance == -1)            
             nearestDistance = hitInfo.distance;

     if (Physics.Linecast(from, clipPlanePoints.UpperRight, out hitInfo) && hitInfo.collider.tag != "Player")
         if (hitInfo.distance < nearestDistance || nearestDistance == -1)
             nearestDistance = hitInfo.distance;

     if (Physics.Linecast(from, clipPlanePoints.LowerRight, out hitInfo) && hitInfo.collider.tag != "Player")
         if (hitInfo.distance < nearestDistance || nearestDistance == -1)
             nearestDistance = hitInfo.distance;

     if (Physics.Linecast(from, clipPlanePoints.LowerLeft, out hitInfo) && hitInfo.collider.tag != "Player")
         if (hitInfo.distance < nearestDistance || nearestDistance == -1)
             nearestDistance = hitInfo.distance;

    if (Physics.Linecast(from,to + transform.forward * -camera.nearClipPlane, out hitInfo) && hitInfo.collider.tag != "Player")
         if (hitInfo.distance < nearestDistance || nearestDistance == -1)
             nearestDistance = hitInfo.distance;
    
     return nearestDistance;
 }

 void UpdatePosition()
 {
     var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth);
     var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth);
     var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth);
     position = new Vector3(posX, posY, posZ);

     transform.position = position;

     transform.LookAt(TargetLookAt);
 }

 void CalculateDesiredPosition()
 {
     //calculate distance
     Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, DistanceSmooth);
     //Get desired position
     desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
 }

 void reset()
 {
     mouseX = 0;
     mouseY = 10;
     Distance = startDistance;
     desiredDistance = Distance;
 }

 void HandlePlayerInput()
 {
     var deadZone = 0.0001f;

     if (Input.GetMouseButton(1))
     {
         //Right mouse button down, get axis input
         mouseX += Input.GetAxis("Mouse X") * X_mouseSensitivity;
         mouseY -= Input.GetAxis("Mouse Y") * Y_mouseSensitivity;
     }
     //Limit mouseY
     mouseY = Helper.ClampAngle(mouseY, y_MinLimit, y_MaxLimit);

     //Checking whether mouse wheel returns value ouside deadzone bounds
     if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone)
     {
         desiredDistance = Mathf.Clamp(Distance - Input.GetAxis("Mouse ScrollWheel") * mouseWheelSensitivity, 
                                       DistanceMin, DistanceMax);
     }
 }

 public static void UseExistingOrCreateNewMainCamera()
 {
     GameObject tempCamera;
     GameObject targetLookAt;
     TP_Camera myCamera;

     if (Camera.mainCamera != null)
     {
         tempCamera = Camera.mainCamera.gameObject;
     }
     else
     {
         tempCamera = new GameObject("Main Camera");
         tempCamera.AddComponent("Camera");
         tempCamera.tag = "MainCamera";
     }

     tempCamera.AddComponent("TP_Camera");
     myCamera = tempCamera.GetComponent("TP_Camera") as TP_Camera;

     targetLookAt = GameObject.Find("targetLookAt") as GameObject;

     if(targetLookAt == null)
     {
         targetLookAt = new GameObject("targetLookAt");
         targetLookAt.transform.position = Vector3.zero;
     }

     myCamera.TargetLookAt = targetLookAt.transform;
 }    
     

}

}

Comment
Add comment · Show 1
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 NAKAS · May 07, 2013 at 09:25 PM 0
Share

I don't know what exactly will work but you could try using a configurable joint or eular angles to limit the rotation.

maybe these links will help http://forum.unity3d.com/threads/81352-Limiting-Rotation http://answers.unity3d.com/questions/169197/euler-angle-problems-with-rotation-limit-script.html http://instantiateunity.blogspot.com/2011/10/limiting-rotation-of-object.html

0 Replies

· Add your reply
  • Sort: 

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

13 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

Related Questions

Object rotates with camera 2 Answers

Return Camera Rotation Z axis to 0 2 Answers

Rotation always changing values! 1 Answer

Wonky Camera Behaviours 0 Answers

Screen.lockCursor messes my rotation 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