https://www.youtube.com/watch?v=m-J5sCRipA0 - this tutorial by brackeys shows how rotate your 2d gun using your mouse. How can I edit this script to add an auto rotating system. Where the gun keeps rotating and when you press a certain button(i.e. space) it stops and shoots.
This is the code for rotating your 2d gun using your mouse;
void Update(){
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; // subtracting the position of the player from the mouse mous position
difference.Normalize(); // normalizing the vector. meaning that all the sum of the vector will be equal to 1
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; //find the angle in degrees
transform.rotation = Quaternion.Euler(0f, 0f, rotZ );
}
Edit: I solved my previous question, but I want it to go anti clockwise after it reaches 0 degrees or when the gun faces right and clockwise again when it reaches 180 degrees or faces left.
this is my updated code -:
void Update(){
transform.Rotate(0, 0, -3.0f);
}