kasquer
October 17, 2016, 11:53am
1
Hello, I ran into a problem while making camera script and am unsure on how to fix it myself. I am using transform.RotateAround to make the camera look up and down based on mouse movement but I don’t want the camera to be able to do a full circle. How would I go about clamping this rotation?
Rotation I want to clamp:
transform.RotateAround (Ship.localPosition, transform.right, -Input.GetAxis("Mouse Y") * Time.deltaTime * 200);
I did some research and found this:
Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
Sot his would be the best code for you
//[SerializeField] show private variable in editor
[SerializeField] private float maxRotX = 10; // max rotation angle
[SerializeField] private float minRotX = -10; // min rotation angle
[SerializeField] private Transform target; // target (ex: ship)
private Vector3 initialVector = Vector3.forward;
void Start () {
initialVector = transform.position - target.position;
initialVector.y = 0;
}
void Update () {
float rotateDegrees = 0f;
float mouseYDelta = -Input.GetAxis("Mouse Y");
float speed = Time.deltaTime * 200;
if (mouseYDelta > 0) { // up
rotateDegrees += speed;
} else if (mouseYDelta < 0) { // down
rotateDegrees -= speed;
}
Vector3 currentVector = transform.position - target.position;
currentVector.x = 0;
float angleBetween = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).x > 0 ? 1 : -1); // calculating angle
float newAngle = Mathf.Clamp(angleBetween + rotateDegrees, minRotX, maxRotX); // clamping angle
rotateDegrees = newAngle - angleBetween;
transform.RotateAround(target.position, Vector3.right, rotateDegrees); // rotation
}
Hope this works out for you.