Realm rush Smooth movement challenge

After some head scratching, i finally achieved this solution:

   [SerializeField] float speed = 10f;

    List<Waypoint> path;
    int waypointCounter = 0;

    void Start()
    {
        path = FindObjectOfType<Pathfinder>().GetPath();
    }

    private void Update()
    {
        MoveToWaypoint(path[waypointCounter]);
    }

    private void MoveToWaypoint(Waypoint waypoint)
    {
        Vector3 groundPos = new Vector3(transform.position.x, 0f, transform.position.z);
        Vector3 direction = (waypoint.transform.position - groundPos).normalized;

        float distanceThisFrame = speed * Time.deltaTime;

        if (distanceThisFrame >= Vector3.Distance(waypoint.transform.position, groundPos))
        {
            waypointCounter++;
            return;
        }

        transform.Translate(direction * distanceThisFrame, Space.World);
    }

I use a “virtual position” (groundPos) that allows me to set different heights for future different types of enemies, thus keeping the grid based position and the actual position of the ship independent from each other.

I have some questions though:

  • I’ve seen solutions from other people in the Q&A section of the course (Lesson 156) and all of them use nested coroutines called in the start method. Are there any differences in performance in doing so?

  • I can’t find a solution to just loop through the list of waypoints in with a for statement instead of keeping track of the waypointCounter as a member variable. Any hint?

  • Difference in performance/usage between Vector3.Translate and Vector3.MoveTowards ?

Thanks alot.

Good job on developing your own solution. :slight_smile:

To be honest, I don’t know. This is something you will have to check in the Profiler. However, if you do not notice any performance issue in your game, I would not worry about your solution because you do not use or calculate anything difficult there.

I’m not sure if I understood your question correctly. You can iterate over a list like you would iterate over an array. A for-loop has got a control variable which you can use. Bear in mind, though, that Update() is a loop itself. It does not pause your for-loop. For this reason, I don’t know what you intend to do with a for-loop.

Since most of Unity is not open-source, I’m afraid nobody but Unity developers will be able to answer this question. You could watch thousands of videos on Youtube and read all articles on Unity there are. Maybe you can find some information about this.

Sounds time consuming? It is. For this reason, worry about the performance only if you have a problem with the performance. Read the official docs on performance optimisation and watch the official United videos on this topic. Then you’ll get a good idea of where you should start if you need to optimise the performance. And make the Unity Profiler your new best friend.

1 Like

By for-loop i mean moving the MoveToWaypoint method to somewhere else, in order to avoid the member variable waypointCounter. My goal here (and it is just a self-challenge to better understand scripting in general) is to NOT increase the waypointCounter explicitly with waypointCounter++, using some sort of foreach/for loop. I understand that Update() is itself a loop. In fact, i tried iterating through waypoints in there and didn’t work :sweat_smile:. I am now looking for a new place to put the call in, and nested coroutines in Start() seems the only solution i can think of with my knowledge.

I know all of this might seem unnecessary, but i really like changing the order of things to better understand them.

Holy words, but again i was just asking for curiosity. Maybe i got a little bit over worried about those things. I’ll take a step back :upside_down_face:.

Thanks a lot and sorry for the wall of text.

The problem with the for-loop is that you cannot pause it. Each iteration step gets executed within a few milliseconds. And when the code block was executed, all local data get lost.

You could use a for-loop in a coroutine with yield return WaitForSeconds(x); with a loop within the coroutine method. As long as the coroutine is running, its local data persists.

This topic was automatically closed after 13 days. New replies are no longer allowed.

Privacy & Terms