I am making a CCTV camera and want to create its rotating behavior once it gets to its maxRotation angle it should reverse and rotate towards minRotation angle and vice-versa. i am using this code but it doesn’t seem to work the camera stucks at maxRotation angle but doesnt reverse to minRotation angle. how can i solve it??
public float minRotation = -45;
public float maxRotation = 45;
public float rotSpeed = 50f;
private void Update()
{
cctvTurnBehaviour();
}
private void cctvTurnBehaviour()
{
// if(Mathf.Approximately(transform.localRotation.y,minRotation))
if((int)transform.rotation.y >= (int)minRotation)
transform.Rotate(new Vector3(0f, rotSpeed * Time.deltaTime, 0f));
else
transform.Rotate(new Vector3(0f, -rotSpeed * Time.deltaTime, 0f));
LimitRotation();
}
private void LimitRotation()
{
Vector3 currentRotation = transform.localRotation.eulerAngles;
currentRotation.y = Mathf.Clamp(currentRotation.y, minRotation, maxRotation);
transform.localRotation = Quaternion.Euler(currentRotation);
}
I just eddited it and its working @KittenSnipes used a bit of your code thanks for it ^^.
@bhavinbhai2707 try this now it worked on my computer.
public class cctv : MonoBehaviour
{
public bool moveRight = false;
private Vector3 rodar;
private void Start()
{
rodar = new Vector3(0f, 0f, 0f);
transform.eulerAngles = rodar;
}
void Update()
{
if (moveRight == false)
{
Debug.Log(transform.rotation.y);
rodar = new Vector3(0f, -50f*Time.deltaTime, 0f);
transform.Rotate(rodar);
if (transform.rotation.y >= 0.20f && transform.rotation.y <= 0.3f)
{
moveRight = true;
}
}
if (moveRight == true)
{
Debug.Log(transform.rotation.y);
rodar = new Vector3(0f, 50f * Time.deltaTime, 0f);
transform.Rotate(rodar);
if(transform.rotation.y <= -0.39f && transform.rotation.y >= -0.5f)
{
moveRight = false;
}
}
}
}
I think this script should work for that. I typed it on my phone so hopefully it helps in some way. Cheers.
public float maxRotation = 180;
public float minRotation = 0;
public float rotateSpeed = 10;
void Update() {
if (transform.rotation.y >= maxRotation) {
rotateDirection *= -1;
}
if (transform.rotation.y <= minRotation) {
rotateDirection *= -1;
}
transform.Rotate(0, rotateDirection * rotateSpeed * Time.deltaTime, 0);
}