- Home /
CCTV behavior rotation issue
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);
             }
     
     
Answer by KittenSnipes · Feb 01, 2018 at 11:31 AM
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);
 }
not working. it works very rarely but shows wierd behavior when works(it rotates from 0 to -180 clockwise and then goes anticlockwise) or else it goes in circle
Use Euler angles and keep between the max and $$anonymous$$ rotation I’ll post an example if needed
Here is an updated version of @Obsessi0n script that is easier to modify. Hopefully it works to your liking. @bhavinbhai2707
 private bool moveRight = false;
 public float speed = 0.25;
 public float maxRotation = 180;
 private Vector3 rodar;
 private void Start()
 {
     rodar = new Vector3(0f, 0f, 0f);
     transform.eulerAngles = rodar;
 }
 void Update()
 {
     if (moveRight == false)
     {
         Debug.Log(transform.eulerAngles.y);
         rodar = new Vector3(0f, -maxRotation * speed * Time.deltaTime, 0f);
         transform.Rotate(rodar);
         if (transform.eulerAngles.y >= maxRotation && transform.eulerAngles.y > 0)
         {
             moveRight = true;
         }
     }
     if (moveRight == true)
     {
         Debug.Log(transform.eulerAngles.y);
         rodar = new Vector3(0f, maxRotation * speed * Time.deltaTime, 0f);
         transform.Rotate(rodar);
         if (transform.eulerAngles.y > maxRotation && transform.eulerAngles.y < 360)
         {
             moveRight = false;
         }
     }
 }
@$$anonymous$$ittenSnipes it worked perfectly fine mate, absolutely the way i wanted. i still don't know why if conditions in previous scripts were not getting executed, and also i need to research a bit on euler angles anyways thanks alot mate u saved my day :)
No problem glad I could help. Cheers
Answer by Obsessi0n · Feb 01, 2018 at 11:24 AM
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;
             }
         }
 
    
     }
 }
Doesn't seem to work, its is behaving like the clocks hand(like ticking) and snaps directly to $$anonymous$$rotation once it reaches maxRotation.
I made the code simpler and tested it and yes it is snapping back to 0 I dont know why I wil try to find out.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                