When rotating with specific angle it's rotating the object to the opposite direction by 180 degrees

private Update()
{
float rotationY = 10 * Mathf.Sin(Time.time * speedRot);
               transformToMove.eulerAngles = new Vector3(transformToMove.rotation.x, rotationY, transformToMove.rotation.z);
}

transformToMove is a child but it’s still not giving me a clue why it’s first changing the direction by 180 degrees and only then start rotating by 10 degrees ?

How should I use with eulerAngles instead rotation ?

Don’t set eulerAngles directly, and don’t read from them except trivially in known stable conditions such as in Start(). The reason: gimbal lock.

ALSO, reading the .x, .y and .z of the rotation has absolutely nothing to do with angles. Those are the interior components of a quaternion, x,y,z,w. Ignore them, you don’t want those. They are black magic.

Instead, take your rotationY and do this:

transform.localRotation = Quaternion.Euler( 0, rotationY, 0);

If that rotation is already non-identity, parent another Transform in above or below it so you can rotate this one transform cleanly.

1 Like

When it’s reaching the takeoff height in this case 7 then it should move from side to side like floating and idle effect but first time when it’s reaching height 7 it’s making sharp turn by 10 degrees to one of the sides then start the rotating from side to side smooth.

How can I make it to start rotating smooth by 10 degrees limit from side to side smooth when it’s reaching the height 7 when taking off ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimFlyertransportationController : MonoBehaviour
{
    public Transform transformToMove;
    public GameObject JetFlame;
    public float speed;
    public bool takeOffLand = false;

    public float speedRot;

    private Animator[] animators;

    private void Awake()
    {
       
    }

    private void Start()
    {
        TurnAllAnimtorsOff(transform, false);
    }

    private void Update()
    {
        if (takeOffLand)
        {
            TurnAllAnimtorsOff(transform, true);
            JetFlame.SetActive(true);

            TakeOff();

            if (Mathf.Approximately(transformToMove.localPosition.y, 7))
            {
                float rotationY = 10 * Mathf.Sin(Time.time * speedRot);
                transformToMove.localRotation = Quaternion.Euler(0, rotationY, 0);
            }
        }
        else
        {
            Land();

            if (Mathf.Approximately(transformToMove.localPosition.y, 0))
            {
                TurnAllAnimtorsOff(transform, false);
                JetFlame.SetActive(false);
            }
        }
    }

    private void TakeOff()
    {
        float step = speed * Time.deltaTime;
        var v3 = new Vector3(transformToMove.localPosition.x, 7, transformToMove.localPosition.z);
        transformToMove.localPosition = Vector3.MoveTowards(transformToMove.localPosition, v3, step);
    }

    private void Land()
    {
        float step = speed * Time.deltaTime;
        var v3 = new Vector3(transformToMove.localPosition.x, 0, transformToMove.localPosition.z);
        transformToMove.localPosition = Vector3.MoveTowards(transformToMove.localPosition, v3, step);
    }

    private void TurnAllAnimtorsOff(Transform root, bool onOff)
    {
        animators = root.GetComponentsInChildren<Animator>(true);

        foreach (Animator a in animators)
        {
            if (onOff)
            {
                a.enabled = true;
            }
            else
            {
                a.enabled = false;
            }
        }
    }
}

This is the part that it’s starting rotating from side to side :

if (Mathf.Approximately(transformToMove.localPosition.y, 7))
            {
                float rotationY = 10 * Mathf.Sin(Time.time * speedRot);
                transformToMove.localRotation = Quaternion.Euler(0, rotationY, 0);
            }

Line 38 just uses Time.time at whatever random value it has reached by that point. That makes the sharp jerk.

Instead, keep your own float and only advance it when you want to, advancing it usually by Time.deltaTime;

Make sure you’re not going up fast enough each frame that you go right past the “approximately.”

1 Like

It’s more complex now :

The first problem I had with this model not my own model is that it’s pivot was not in the center of the model but at the bottom under the model so I created a new empty GameObject and dragged the model as child under the new empty GameObject.

Then I set both children of the model ROOT and Flyer positions to 0,0,0 this fixed the pivot problem and let me use with the player the possible to interact with the model object.

Now back to the script. The script is attached to the Anim_Flyer_Transportation.
When I’m setting the flag bool variable takeOffLand to true the spacecraft is taking off and moving slowly smooth up to height 7.

Then at height 7 I want the spacecraft to float and idle and because the model animation idle is not good for me because it’s changing position of the spacecraft I thought to create on my own idle effect.

No the problems with the rotation part when reaching height 7 :

If the Transform To Move is the Anim_Flyer_Transportation then the model the spacecraft will rotate first by 180 degrees to the opposite direction no matter what I’m doing :

If instead in the Transform To Move I drag the empty GameObject when running the game for some reason the whole object will first fall a bit down to the ground into the ground then will take off when the flag is true and then will start make the rotation at height 7 and will do this jerky effect. So I tried to change it to Time.deltaTime but then it’s not rotating at all.

So dragging the empty GameObject parent to the script it will not rotate by 180 degrees to the opposite direction and when it was Time.time the effect was working but with the jerky problem when changing to Time.deltaTime it’s not rotating at all just staying still.

If you’re gonna do any animating yourself, remove all the animators and animations or you’re just going to tangle yourself up in them. They always win because they get to go last: