- Home /
How do I get this to Rotate properly?
I have this code here and I was trying to make the rotation be clamped so it can't rotate all the way around, but now its just rolling up to a certain point. So how do I get it to where it looks up and down and side to side only up to a certain point on each side.
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class CameraRotate : MonoBehaviour
{
public float speedH = 2.0f;
public float speedV = 2.0f;
private Camera maincamera;
private float yaw = 0.0f;
private float pitch = 0.0f;
private float rotationZ = 0f;
private float rotationX = 0f;
void Start()
{
maincamera = Camera.main;
}
void Update()
{
maincamera.transform.LookAt(transform);
float speed = 10.0f;
rotationX = CrossPlatformInputManager.GetAxis("Horizontal_2") * speed;
rotationZ = CrossPlatformInputManager.GetAxis("Vertical_2") * speed;
rotationX = Mathf.Clamp(rotationX, -90, 90);
rotationZ = Mathf.Clamp(rotationZ, -90, 90);
transform.localEulerAngles = new Vector3(rotationX, transform.localEulerAngles.y, rotationZ);
}
}
Answer by ProtoTerminator · Dec 18, 2016 at 02:53 PM
Parent the camera to another gameobject. That gameobject will be your reference to your clamp. Then, simply use Mathf.Clamp
to clamp your angle
camera.transform.localEulerAngles = new Vector3(Mathf.Clamp(negativeAngle, positiveAngle), Mathf.Clamp(negativeAngle, positiveAngle), Mathf.Clamp(negativeAngle, positiveAngle));
maincamera.transform.localEulerAngles = new Vector3(rotationX, transform.localEulerAngles.y, rotationZ);
Like this? I decided on this option ins$$anonymous$$d however I would still like to be able to stop it from rotating to far. but otherwise it works except for the fact that the camera doesn't stop when it reaches a certain point.
transform.Rotate(Vector3.up, $$anonymous$$athf.Clamp((CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal_2") * speed), -90, 90));
transform.Rotate(Vector3.left, $$anonymous$$athf.Clamp((CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical_2") * speed), -90, 90));
I have another question too related to this one how do clamp the basic rotation of any object like if I want the x to rotate a little bit on the ground the I want that clamped so it doesn't rotate so much as to fall on its back.
Follow this Question
Related Questions
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers
How to fix weird camera rotation? 0 Answers
Camera moving independent of players rotation 1 Answer
Return camera to original rotation values Logic 1 Answer
Limiting vertical camera rotation 0 Answers