- Home /
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;
}
}
}
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
Your answer
Follow this Question
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