Laser Defender, enemy doesn't move

Hi, I just finished Pathfinding lecture in the Laser Defender game course and my enemy chracter doesn’t move. It gets stuck on the edge of the screen. Only one enemy on the second path is moving. Can someone help me and tell me what is the reason? Here’s the link if you’d like to see: https://github.com/xSerj111/Laser-Defender

Hi xSerj,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? If so, what did you figure out? Were you able to identify the exact moment when the enemies get stuck?

1 Like

Hi, I am not sure what to exactly check with Debug Log. The enemy gets stucked on the edge of the main scene and its pathway starts just behind the main scene so it gets stucked just after start. Code is the same like in lecture:

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pathfinder : MonoBehaviour
{
[SerializeField] WaveConfigSO waveConfig;
List waypoints;
int waypointIndex = 0;
void Start()
{
waypoints = waveConfig.GetWaypoints();
transform.position = waypoints[waypointIndex].position;
}

void Update()
{
    FollowPath();
}

void FollowPath()
{
    if (waypointIndex < waypoints.Count)
    {
        Vector3 targetPosition = waypoints[waypointIndex].position;
        float delta = waveConfig.GetMoveSpeed() * Time.deltaTime;
        transform.position = Vector2.MoveTowards(transform.position, targetPosition, delta);
        Debug.Log(delta);
        if (transform.position == targetPosition)
        {
            waypointIndex++;
        }
    }
    else
    {
        Destroy(gameObject);
    }
}

}`

EDIT. It actually happens on Wave 0 set with its path and on Wave 1 it works. I don’t what may be the reason

If the enemies start at the wrong position, you should be able to see that with Debug.Log. Maybe the waypointIndex is wrong. This could also explain why the else block does not get called. Of course, I don’t know if it doesn’t get called because I cannot see what happens at runtime just by reading the code. That’s why I suggested to use Debug.Log. Debug.Log can help you reenact the logic flow at runtime because what we expect when reading the code is not necessarily the same what happens when the code gets executed (if it gets executed).

I checked that waypointIndex stays at 0 in path 0, and that it goes through all indexes when it is path 1. However I have no idea why it is hapenning if you have some advice for me what can I check you’re welcome.

Did you check with Debug.Logs why the waypointIndex does not get incremented? From what I see in the code you shared, the relevant part is wrapped in an if-block:

if (transform.position == targetPosition)
{
    waypointIndex++;
}

Maybe targetPosition.z is not 0f, which is something one could easily check with a Debug.Log in the code. transform.position.z gets set to 0f because of Vector2.MoveTowards. If you verified with Debug.Log that this is indeed the problem, you could test Vector3.MoveTowards.

1 Like

The transform.position.z gets set to 0. When I changed it Vector3.MoveTowards it workes without a problem. Thanks! But can you explain me why?

As aforementioned, transform.position.z gets set to 0f because of Vector2.MoveTowards. If targetPosition.z is not 0f, transform.position == targetPosition is false. This can easily be checked with Debug.Log, the most important method that helps us understand what’s going on in code written for Unity games. If you want to develop your own games one day, it is highly recommended to learn how to use this method to analyse your code.

Privacy & Terms