How to go into a specific direction when rotating with Quaternion.slerp?

Hi mates,

I’m running into a problem since a while and I’m not able to fix it.
I have a script to rotate a Gameobject to known angle using Quaternion.Euler angles inside a Quaternion.Slerp Method.

I planed to use a RotateCW (clockwise) and RotateCCW (counter clockwise) function. But depending on the target angle the object rotates in random (or maybe the shortest?) direction. I Cant control it. It hit’s the target, the problem is just the direction

It starts inside the WpChange Method. I created Quaternion rotAngles and set this to some Euler angles. Than I call RotateCCW() with this angles (It’s just a rotation around the Y Axis)

Here’s a bit of code:

public void WpChange(ModuleScript.Direction direction, float changingTime)
    {
        float timeToRotateCCW = (changingTime / 360) * 90;
        float timeToRotateCW = changingTime / 360 * 180;
        float timeToRotateToOrig = (changingTime / 360) * 90;
        Quaternion rotAngles;

        switch (rotationStep)
        {          
            case 1:
                rotAngles = Quaternion.Euler(pitch, yaw, roll);
                if (RotateCCW(timeToRotateCCW,rotAngles))

Inside the RotateCCW() Method:

public bool RotateCCW(float timeToMove, Quaternion rotAngles)
    {
        if (currentTime <= timeToMove)
        {
            currentTime += Time.deltaTime;
            transform.localRotation = Quaternion.Slerp(startRot, rotAngles, currentTime / timeToMove);
            return false;
        }
        else
        {
            transform.localRotation = rotAngles;
            startRot = transform.localRotation;
            return true; //returns true when object is rotated until the endpoint
        }      
    }

I have currently the exact same code for going in clockwise direction. But currently it doesn’t matter because they both doing what ever they want depending on the traget rotation (of course also bacause of the start angle on some way)

 // Rotate Arm Clockwise 
    public bool RotateCW(float timeToMove, Quaternion rotAngles)
    {
        if (currentTime <= timeToMove)
        {
            currentTime += Time.deltaTime;
            transform.localRotation = Quaternion.Slerp(startRot, rotAngles, currentTime / timeToMove);
            return false;
        }
        else
        {
            transform.localRotation = rotAngles;
            startRot = transform.localRotation;
            return true; //returns true when object is rotated until the endpoint
        }
    }

It would be great if somebody can help me. I messed around now for more than a week, and found no solution.

Thanks,
Marco

I think you should set three different rotations, and only rotate pitch, yaw, or roll at any given time.

Privacy & Terms