Realm Rush - How to Rotate Enemy 90 deg increments in Y to match path?

Hello there!
I am using my own 3D models with animations and would like to move my enemies 90 degrees with each move they make.
I tried implementing some monstrous nested “if” statements and some transform.localRotation * Quaternion.Euler(0f, (numbers like 90f, 180f, 270f, lol), 0f) but none of it really worked. I got frustrated and deleted the monster, but I’m sure it wasn’t correct anyways… I would greatly appreciate it if one of you kind TAs or more experienced devs could shine a little light on the solution.

Currently, I have them programmed to “LookAt” the base (since the rotation was not working at all).
Ideally, I would like to have them face whatever direction they are moving in.

For location, I was thinking it would go in the FollowPath coroutine?

IEnumerator FollowPath(List<Waypoint> path)
{
    print("Starting patrol");
    foreach (Waypoint waypoint in path)
    {
        transform.position = waypoint.transform.position; //would add the code after this?
        yield return new WaitForSeconds(dwellTime);
    }

    print("Ending patrol");
    anim.SetInteger("condition", 1);
    //transform.localRotation = transform.localRotation * Quaternion.Euler(0f,Random.Range(-60f,60f),0f);
}

Thank you so much in advance! I really appreciate the help.
Best,
Terra

Hi Terra,

Your assets looks really nice, and your idea sounds interesting. Thanks for sharing your code and where you initially added the rotation part. That’s very helpful because, from what I read in your description, your code should have worked. From what I see in your code, you were very close to the solution, which is why I won’t tell you what to do right away unless you explicitely ask me.

Howver, I’m giving you a few hints that will hopefully help you find and fix the issue:

Your idea is to rotate the enemy each time it moves, isn’t it? Where is the “each move” part? Add the rotation code there. The enemy should then rotate each time it moves. Maybe it rotates not as expected but that would be another problem.

1 Like

Hello Nina!
Thank you so much for your help!! I successfully got them to rotate randomly, but I still can’t come up with a solution to get them to rotate in the desired direction each time.
I’m satisfied with this for now, though! Maybe when I have more coding experience I can revisit this code and implement what I’ve learned.

Hope you have a great day! :smiley: I really appreciate you being here to help guide us!

What is the desired direction? According to your code, you used Random.Range, so the direction is more or less random. Is this the problem?

If not, is there a reason why transform.localRotation is multiplied by Quaternion.Euler instead of added? And what’s the reason for -60f and and 60f? 180f + 60f = 240f.

1 Like

Hello Nina,

Thank you for the follow up!!! I got it working thanks to your help!!!
It’s still a monster, but it works. Yay!

As for the Random.Range – I had them playing an animation and dealing gradual damage through a Coroutine, but if the player doesn’t place towers then they just look piled up and sad… so I tried to randomize the rotation (which also works now, thanks to your advice about adding the Quaternion.)
I guess as a design choice at this stage, having them do a big attack and disappear like Rick does in the tutorials is a better idea – that would solve the issue of them piling up, though I’m sad my animation will only be able to play once, haha.

Here is the code:


            CheckRotation(waypoint);

            yield return new WaitForSeconds(dwellTime);
        }

        print("Ending patrol");
        anim.SetInteger("condition", 1);
        transform.localRotation = Quaternion.Euler(0f, transform.localRotation.y + UnityEngine.Random.Range(-20f,40f), 0f);
    }

    private void CheckRotation(Waypoint waypoint)
    {
        if (waypoint.exploredFrom == null)
            print("mmkay");

        else if (waypoint.exploredFrom.transform.position.x > waypoint.transform.position.x &&
            waypoint.exploredFrom.transform.position.z == waypoint.transform.position.z)
        {
            transform.localRotation = Quaternion.Euler(0f, transform.localRotation.y - 90f, 0f);
            print("turned left");
        }

        else if (waypoint.exploredFrom.transform.position.x < waypoint.transform.position.x &&
        waypoint.exploredFrom.transform.position.z == waypoint.transform.position.z)
        {
            transform.localRotation = Quaternion.Euler(0f, transform.localRotation.y + 90f, 0f);
            print("turned right");
        }

        else if (waypoint.exploredFrom.transform.position.x == waypoint.transform.position.x &&
        waypoint.exploredFrom.transform.position.z < waypoint.transform.position.z)
        {
            transform.localRotation = Quaternion.Euler(0f, transform.localRotation.y + 0f, 0f);
            print("turned down");
        }

        else if (waypoint.exploredFrom.transform.position.x == waypoint.transform.position.x &&
        waypoint.exploredFrom.transform.position.z > waypoint.transform.position.z)
        {
            transform.localRotation = Quaternion.Euler(0f, transform.localRotation.y + 180f, 0f);
            print("turned up");
        }
        else print("NOOOOOOOOO");
    }

Fantastic. I knew you would be able to make your idea work. :slight_smile:

1 Like

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

Privacy & Terms