# Stopping rotation at 90° and -90°/270°.

I have an Transform that i am rotating backwards through touch commands on android. The rotation itself works fine. What I want to happen though is when it reaches 270 / -90 that it cannot rotate anymore and the same if it reaches 90 degrees the opposite direction. Upon reaching those points i want it to reset back to its zero degree rotation. Any help would be greatly appreciated. Thanks in advanced.

My code:

``````using UnityEngine;
using System.Collections;

public class Pivot : MonoBehaviour {

public float speed = 10.0f;
public Vector2 touchDeltaPosition;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if (Input.touchCount > 0  Input.GetTouch(0).phase == TouchPhase.Moved) {
touchDeltaPosition = Input.GetTouch(0).deltaPosition;
}
}

void LateUpdate () {

if(Input.touchCount > 0  touchDeltaPosition.x <= 0) {
if(transform.rotation.z >= 270  || transform.rotation.z <= 90) {
transform.Rotate(0, 0, -speed );
}
else {
transform.Rotate(0, 0, 270);
}

}
}
}
``````
1. You should move that code to Update. Not sure why you are doing it in LateUpdate (unless you’re overriding animation - in which case carry on)
2. Do you really want to be rotating at 270 on line 30? i’m thinking that was supposed to be speed

This code will snap to 0 when you go out of bounds.

``````float z = transform.eulerAngles.z;
if(z > 180  z < 270)
z = 0;
else if(z < 180  z > 90)
z = 0;
``````

Im trying to get that working right now. And im not looking for it to snap back but rotate back over time. Is basically a pivot point for catapult arm. Thank you very much though.

here’s how I would do it (note: im gonna write this as a desktop script rather than device, but you can replace the inputs as required)

``````void Update()
{

bool drawing = Input.GetKey(KeyCode.Space);

if(drawing) //catapult is pulling back
{
float z = transform.eulerAngles.z;
if((z > 180  z > 270) || (z > 0  z < 90))
transform.Rotate(0,0,-speed * Time.deltaTime);
}
else //firing code - you may want to do something with total rotation distance to determine catapult power
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,20), 270*Time.deltaTime);
}

}
``````

That works perfect then only thing is that when the game starts it kicks it right up to 20 degrees. i want it to start out at zero. Thank you again for your help.

Edit: And if this makes it clearer, what i called a catapult is a boot on a stick and that is attached as a child to this pivot point. So the force for what its going to kick will be based in another script. I just need to figure out how to save a variable of some kind to determine the rotation of the pivot when it is released. Sorry im newer o this kind of math and really appreciate the help.

Yeah I made it rotate to 20 when not firing (assuming a catapult action would go past zero degrees when it does fire)… just change the 20 to 0.

as for the rotation distance… Unity - Scripting API: Mathf.DeltaAngle

float z = transform.eulerAngles.z;
float distance = Mathf.Abs(Mathf.DeltaAngle(z, 0));

Trying that now, and yes i do want it to go past 0, all the way to 90 just depending on how far back(between 0 and 270/-90) it was pulled. Then multiply that by a force multiplier(float) then ad that new number as force to the object its kicking. I hope im describing this okay.

Edit, i tried changing it to 0 and it stopped cranking back all together.

Something like this should be close to what youre after

``````bool fired = true;
bool firing = false;
float power = 0;

void Update()
{
if(fired)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,0), speed*Time.deltaTime);
if(Mathf.Approximately(transform.rotation.z, 0))
fired = false;
}
else
{
bool drawing = Input.GetKey(KeyCode.Space);
float z = transform.eulerAngles.z;
if(!firing  drawing) //catapult is pulling back
{
if((z > 180  z > 270) || (z > 0  z < 90))
transform.Rotate(0,0,-speed * Time.deltaTime);
}
else //firing code
{
if(!firing)
{
firing = true;
power = Mathf.Abs(Mathf.DeltaAngle(z,0);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,power), 270*Time.deltaTime);

if(Mathf.Approximately(transform.rotation.z, power))
{
fired = false;
firing = false;
}
}
}
}
``````

Ive been trying to get that new code working. When i hold space nothing happens at all:(

Edit: after playing around with it. It seems the firing boolean is never turning off during runtime.

opps, try changing the last fired = false, to fired = true