Clamping Between Two Different Ranges
Hello, I commented the code below to make it easier. I am trying to make a free rotating camera. I've got most of it, but now I want to limit it so the user can't flip the camera upside down. I am using RTSControl.transform.eulerAngles = rotation to achieve this. The problem is that if I'm using this method I would need to clamp between either 315 and 0 ... OR ... between 0 and 45. How can I clamp between two ranges like this, or can someone else explain a good way of achieving this? Thank you very much
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class CameraController : MonoBehaviour
{
GameObject RTSControl;
public Camera rtsCamera;
private void Start()
{
RTSControl = GameObject.Find("RTSControl");
rtsCamera = GameObject.Find("rtsCamera").GetComponent<Camera>();
}
void Update()
{
//Lets me rotate all the way in all directions
//However also lets me rotate until the camera is upside down!
if (Input.GetMouseButton(1))
{
float yaw = Input.GetAxis("Mouse X");
float pitch = Input.GetAxis("Mouse Y");
RTSControl.transform.RotateAround(RTSControl.transform.position, Vector3.up, yaw * 500f * Time.deltaTime);
RTSControl.transform.Rotate(-pitch * 500f * Time.deltaTime, 0, 0);
}
//Clamps the rotation between 0 and 90
//However I need it between 315 and 0 ... OR between 0 and 45
Vector3 rotation = RTSControl.transform.eulerAngles;
rotation.x = Mathf.Clamp(rotation.x, 0, 90);
RTSControl.transform.eulerAngles = rotation;
}
}
Answer by Sixtoo · May 01, 2018 at 08:32 PM
I'm still looking for a better way to clamp between two different ranges... but I've come up with a very poor solution to the problem in the mean time. Basically I store the last known angle that is in range, and if the camera rotation goes out of range then it snaps back to the last known "good" angle. Unfortunately this causes the camera to stutter when going out of the range.
Vector3 lastGoodAngle = new Vector3();
void Update()
{
Vector3 rot = RTSControl.transform.eulerAngles;
//Need to find a better way to limit rot.x angle
if (Input.GetMouseButton(1) && (rot.x < 45 || rot.x > 320))
{
float yaw = Input.GetAxis("Mouse X");
float pitch = Input.GetAxis("Mouse Y");
RTSControl.transform.RotateAround(RTSControl.transform.position, Vector3.up, yaw * 500f * Time.deltaTime);
RTSControl.transform.Rotate(-pitch * 500f * Time.deltaTime, 0, 0);
lastGoodAngle = rot;
}
else
{
RTSControl.transform.eulerAngles = lastGoodAngle;
}
}
I'm using this because for some reason when I tried to Mathf.Clamp the angle and set the transform.eulerAngles the camera goes skew. I think it may have been because I was trying to only set the rot.x value without recalculating the whole transform.eulerAngles ? I don't know.
Still looking for a solution to clamp the value between one of the two different ranges
Your answer
Follow this Question
Related Questions
Camera X Rotation Problems 0 Answers
Limit camera rotaion on x axis euler angles 1 Answer
UI compass pointing to a 3D object 0 Answers
transform.eulerAngles problem with parent rotation 0 Answers
Limit Cam rotation 0 Answers