Lerped Rotation of Enemy

I modified the coroutine to also lerp the rotation of the Ram instead of using the transform.LootAt() method. Was hoping to get some feedback on whether this is a good implementation for lerping rotation:

    IEnumerator FollowPath()
    {
        //loops through List of waypoints and executes the script for each waypoint.
        foreach(Waypoint waypoint in path)
        {
            float time = 0f;
            float duration = 1f;
            Vector3 targetDir = waypoint.transform.position - transform.position;

            Quaternion newRot = Quaternion.LookRotation(targetDir, Vector3.up);

            while (transform.rotation != newRot)
            {
                time += Time.deltaTime;
                transform.rotation = Quaternion.Lerp(transform.rotation, newRot, time / duration);
                yield return null;
            }

            if (transform.rotation == newRot)
            {
                Vector3 startPos = transform.position;
                Vector3 endPos = waypoint.transform.position;
                //transform.LookAt(endPos);
                time = 0f;

                while (time<duration)
                {
                    time += Time.deltaTime * speed;
                    transform.position = Vector3.Lerp(startPos, endPos, time / duration);
                    yield return new WaitForEndOfFrame();
                }
            }
        }

https://thumbs.gfycat.com/SimilarPeriodicBelugawhale-size_restricted.gif

6 Likes

Hi Sung-jin,

Welcome to our community! :slight_smile:

Your gif animation looks nice. Well done!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms