Simple question

Hello All,

I have a simple question:

How do I rotate a transform 180 in the Y and stop?

I’ve always moved objects and never tried to just rotate something 180 and stop. If I do something like this:

transform.Rotate(0, 180, 0);

it will rotate for ever? because its in the Update function.

Cheers,
Zeek

using UnityEngine;
using System.Collections;

public class RotationScript : MonoBehaviour
{
    public float angle = 180.0f;
    public float speed = 2.0f;

    public void Start()
    {
        StartCoroutine("Rotate");
    }

    public IEnumerator Rotate()
    {
        Vector3 axis = new Vector3(0.0f, 1.0f, 0.0f);
        float rotPercent = 0.0f;
        float totalRot = 0.0f;

        Quaternion startRot = transform.rotation;

        while (totalRot < 1.0f)
        {
            rotPercent = Mathf.SmoothStep(0.0f, 1.0f, speed * Time.deltaTime);
            totalRot += rotPercent;
            transform.Rotate(axis, rotPercent * angle);
            yield return 0;
        }
        transform.rotation = Quaternion.Euler(new Vector3(startRot.eulerAngles.x, startRot.eulerAngles.y + angle, startRot.eulerAngles.z));
    }
}

Wow thats a lot of stuff to rotate an object 180, I thought there would be something simpler. dont get me wrong I really do appreciate the reply especially for taking the time to put together the code.

hmm… maybe I should try and explain it another way. How would I flip an objects transform in world space or actually make its “transform rotation” be from 0, 180, 0, (xyz) to be 0, 0, 0, in world space.

Im searching and I’ve ust stumbled upon this

transform.rotation = Quaternion.identity;

hmm… I’ve gotta talk to it position… I’ll write back Im gonna try a few other things

Hello, if you want it to be instant just use transform.eulerAngles.y = 180 or 0 to return to original position.

if you want to make it over time you can do something like

var Speed : float;

function FixedUpdate () {

if (transform.eulerAngles.y < 180) {

transform.rotate (Vector3.up * Speed * Time.deltaTime)

}
}

Awesome Thank you LoneWolfGabo :slight_smile: that did the trick :smile:

I love these forums what a great place I never used eulerAngles before but Im reading up on it now :slight_smile: again Greatly Appreciated!