0
\$\begingroup\$

This is before rotating the player is facing the target :

Facing target

Then rotating but not facing me the player it seems like it's not finishing the rotating ?

When the rotating ended

In the script in the Update :

private void Update()
    {       
        if(rot)
        {
            StartCoroutine(ScaleOverSeconds(player.localScale,
                new Vector3(0, 180, 0), player.localPosition, 1.5f));
            rot = false;
        }
    }

Then :

public IEnumerator ScaleOverSeconds(Vector3 scaleTo, Vector3 rotateTo, Vector3 moveTo, float seconds)
    {
        float elapsedTime = 0;
        Vector3 startingScale = player.localScale;
        Vector3 startingRotation = player.localEulerAngles;
        Vector3 startingPosition = player.localPosition;

        while (elapsedTime < seconds)
        {
            player.localScale = Vector3.Lerp(startingScale, scaleTo, (elapsedTime / seconds));
            player.localEulerAngles = Vector3.Lerp(startingRotation, rotateTo, (elapsedTime / seconds));
            player.localPosition = Vector3.Lerp(startingPosition, moveTo, (elapsedTime / seconds));

            elapsedTime += Time.deltaTime;

            yield return null;
        }
        player.localScale = scaleTo;
        player.localEulerAngles = rotateTo;
        player.localPosition = moveTo;
    }

In this case, I want the player only to rotate without scaling or changing position so I used layer.localScale and the player.localPosition when starting the Coroutine.

\$\endgroup\$
3
  • \$\begingroup\$ Are you sure you start the rotation from 0,0,0. If your initial orientation is not 0, 0, 0 then it will rotate less than 180degrees. \$\endgroup\$ – Nikaas May 4 at 6:18
  • \$\begingroup\$ @Nikaas Let's assume that the rotation is no 0,0,0 is there a way to make it always rotate to facing the camera or the player? or only if it's starting at 0,0,0 it will be accurate? \$\endgroup\$ – Haim Hadad May 4 at 6:52
  • 1
    \$\begingroup\$ If you want to rotate a certain amount (as opposed how it's now, toward certain hardcoded direction) you need to pass current rotation + delta rotation for your rotateTo parameter. In your case localRotation + new Vector3(0,180,0) will always turn 180 degrees. If your camera is not always at the character back you can take camera (using say Camera.main) and pass its rotation as rotateTo. But you probably will have to look how to do world to local rotation conversion. BTW I would change the coroutine to accept not local but absolute values. \$\endgroup\$ – Nikaas May 4 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.