# How do i rotate an object 90 or -90 degrees upon collision?

I don’t really have a script to show you much, because i’ve been through many different ones that haven’t worked. But i am trying to rotate an object smoothly by 90 degrees, or -90 on the Y axis every time it collides with an object… this is all i have so far:

``````public GameObject rotatedObject;
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "Arrow" || col.gameObject.name == "Fire Arrow")
{
//Rotate rotatedObject by 90 degrees on the Y axis
}
}
``````

Any Help would be greatly appreciated! Thanks

public GameObject rotatedObject;
bool rotating = false;
public float smoothTime = 5.0f; //rotate over 5 seconds

``````    void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Arrow" || col.gameObject.name == "Fire Arrow" && !rotating) // we dont want to call this if the object is already rotating, so we check if it is
{
//Rotate rotatedObject by 90 degrees on the Y axis
rotating = true;
float rando = Random.Range(0, 100); // pick a random number between 1 and 100
int multiplier = 1;
if (rando > 50)
{
multiplier = -1;
}
StartCoroutine(RotateOverTime(rotatedObject.transform.localEulerAngles.y, rotatedObject.transform.localEulerAngles.y + (90 * multiplier), smoothTime);
}
}
IEnumerator RotateOverTime(float currentRotation, float desiredRotation, float overTime)
{
float i = 0.0f;
while (i <= 1)
{
rotatedObject.transform.localEulerAngles = new Vector3(rotatedObject.transform.localEulerAngles.x, Mathf.Lerp(currentRotation, desiredRotation, i), rotatedObject.transform.localEulerAngles.z);
i += Time.deltaTime / overTime;
yield return null;
}
yield return new WaitForSeconds(overTime);
rotating = false; // no longer rotating
}
``````

public bool turning;
private float targetRot_y;
public float turnSpeed = 1f;

``````public GameObject rotatedObject;

void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "Arrow" || col.gameObject.name == "Fire Arrow")
{
int rn = Random.Range(0,2);
if(rn == 0) {
targetRot_y = 90;
}
else {
targetRot_y = -90;
}
if(turning == false) 7
StartCoroutine("Turn");
}

private IEnumerator Turn() {
turning = true;
Quaternion rot_old= rotatedObject.transform.rotation;
rotatedObject.transform.Rotate(0, targetRot_y, 0);
Quaternion rot_new = rotatedObject.transform.rotation;

for (t = 0.0f; t <= 1.0f; t += (turnSpeed * Time.deltaTime)) {
rotatedObject.transform.rotation = Quaternion.Slerp(rot_old, rot_new , t);
yield return null;
}

rotatedObject.transform.rotation = rot_new ;
turning = false;

}
``````

As you can see from the answers, there are many ways to do this.

``````	//public GameObject rotatedObject;
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "Arrow" || col.gameObject.name == "Fire Arrow")
{
//Rotate rotatedObject by 90 degrees on the Y axis
rotateLerpTime = 0;
rotateStart = transform.localRotation;
rotateTarget = Quaternion.Euler(transform.localRotation.eulerAngles.x,
transform.localRotation.eulerAngles.y + (Random.Range(0,2) == 0 ? 90 : -90),
transform.localRotation.eulerAngles.z);

}
}

float rotateLerpTime;
Quaternion rotateStart, rotateTarget;
void Update()
{
//transform.position += transform.forward * Time.deltaTime;

if (!Mathf.Approximately(transform.localRotation.eulerAngles.y, rotateTarget.eulerAngles.y))
{
rotateLerpTime = Mathf.Clamp(rotateLerpTime + Time.deltaTime, 0f, 1f);
transform.localRotation = Quaternion.Slerp(rotateStart, rotateTarget, rotateLerpTime);
}
}
``````