[Solved] Rotate object on one axis to certain value accurately

Hey guys,

I want to rotate an object around it’s Z axis as long as a button is pressed. I need to rotate it from 0f to -40f and not further. How would I do that? My current code isn’t doing the trick and I also think it won’t work accurate.

Vector3 FinalPos = new Vector3(AirbrakeLeftController.eulerAngles.x, AirbrakeLeftController.eulerAngles.y, -40f);
        if (Vector3.Distance(AirbrakeLeftController.eulerAngles, FinalPos) > 0.01f)
        {
            AirbrakeLeftController.eulerAngles = Vector3.Lerp(FinalPos, AirbrakeLeftController.rotation.eulerAngles, -Time.deltaTime * .02f);
        }
        else
        {
            AirbrakeLeftController.eulerAngles = FinalPos;
        }

Any good ideas?

Maybe something like that?

    public void Update()
    {

        Vector3 FinalPos = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, -40f);
        float speed = 1;
        if (transform.eulerAngles.z > FinalPos.z + 0.1f)
        {
            transform.Rotate(0, 0, speed  * - Time.deltaTime);
        }
        else if(transform.eulerAngles.z < FinalPos.z - 0.1f)
        {
            transform.Rotate(0, 0, speed * Time.deltaTime);
        }
        else
        {
            transform.eulerAngles = FinalPos;
        }
    }

Hm, didn’t completely do the trick. It keeps rotating the object endlessly. I might need to add that I execute the method while a button is pressed.

I edited my post, did you try the new or the old?
if that never stop with this code, try delete the 0.1f and replace with 10f for see if the probleme is the precision, also set the speed lower.

I saw it, thanks.

I am also seeing another issue why I am failing. During setup, I have the Z rotation of the object at 0. But when starting the game in Unity, eulerAngles.z returns me 359.735 or similar values… Is there a way to read the actual 0 that is also still shown in the Inspector during runtime?

You can do it with a float, a clamp, and setting the rotation.

float max = 0, min = -40f;
float angle = 0;
// while button pressed, decrease the value by speed * Time.deltaTime
angle -= speed * Time.deltaTime;
angle = Mathf.Clamp(angle, min, max);
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, angle);

Pretty sure that should work. :slight_smile:

Your best solution is to round it.

@ElFab I adapted your method with some more checks and it works nicely now.

@methos5k Your code looks even simpler, and of course it also works perfectly :slight_smile:

Thanks to both of you for your time and solutions!

No problem. I was almost not going to say anything, I knew the other solution would work with a few changes. Just thought I’d share it as another option/thought :slight_smile:

Have fun with your game :slight_smile:

Hmm to be honest I shot too eartly :smile:

Your solution does work but it has one issue. When I do not use the method, the Z rotation of the object is now 0 to world. It looks like it’s not parented any more.
For better understanding: It’s an airbrake of an aircraft. It would now always rotate parallel to the ground on Z axis, no matter the rotation of it’s parent [the aircraft].

I assume it’s just a simple fix, but… :smile:

change rotation to localRotation, if that’s what you mean.

** if you’re talking to me lol.

Yes, exactly. Fixed it, thank you.
Guess I need a break :slight_smile:

Thread can be closed!

heh, cool. Just for the record, threads don’t get closed but you can write ‘[Solved] thread_Title’ (rename your thread!) I mean :wink:

Glad it worked.