Whats going on? the ship just vanished

im following the unity laser defence course and we are on the section that I covering enemy waypoints/paths… the code for the enemypathing.cs, was all working ok until the last two lines of code. now when I start the project with in unity the enemy ship just disappears it can’t bee seen in the game screen or the project screen. if I turn off the script completely the ship remains. any ideas would be good??. I’m using unity version 2020.2.3f1 is this a bug issue?? as I believe the prefab is not updating

Hi,

Is the z-position of the background set to 10, the z-position of the camera to -10 and the z-position of the other game objects to 0?

And try to replace Vector2.MoveTowards with Vector3.MoveTowards in the EnemyPathing class.

Did this fix it?

yes the issue was with the vector2.MoveToward… by changing this to vector3.MoveTowards the ship now cover all 3 path points as shown in the course. all camera and background setting where set correctly. I have tried updating the prefab and that is still stubborn and won’t update.
on the down side the last two parts of the code are still coursing the ship to disappear… this is when the ship has completed the waypoints and you ask it to destroy at last waypoint???

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

problematic code???

After a bit of tinkering I don’t know if this helps.but if I do not use the Destroy(gameObject) line in the code all is well. as soon as I use that line then the ship disappears is the ship being destroyed before it has done the waypoints???

ok I have a bit of a work around… if you uses the shredder to destroy your ships it is just an extra waypoint to add. if you surround your play space with a shredder. i.e 1 at top, bottom, left and right then any ships you miss should be destroyed and if you place the shredder far enough you can still circle remaining ship round for another pass before sending them off to the shredder… in a not so course way all should be good. IMPROVES, ADAPT and OVERCOME I guess…

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? The ship is supposed to get destroyed at the last waypoint.

A shredder is a valid solution.

Apropos shredder, check if there are any game objects in your game which might destroy the enemies. Some students hat placed their first waypoint inside the shredder at the top and the bottom of their game space. That shredder immediately destroyed their spawned enemies.

it is strange I have rechecked and rewritten the code… that same thing…??? I don’t start the ship in the shredder…lol

Add Debug.Logs to your code to figure out what might be destroying your enemy. If your enemy collides with something, you could log the name of the colliding game object into your console.

ok I have check the notes in the course a ppl are having the same trouble as I was…
there is a code rewrite that solves this problem.
if (waypointIndex <= waypoints.Count + 1)

    {
        var targetPosition = waypoints[waypointIndex].transform.position;
        var movementThisFrame = enemySpeed * Time.deltaTime;

        transform.position = Vector3.MoveTowards
            (transform.position, targetPosition, movementThisFrame);

        if (transform.position == targetPosition)
        {
            waypointIndex++;
        }
    }
    if (waypointIndex == waypoints.Count)
    {
        Destroy(gameObject);
    }
}

but I won’t work with vector2 only vector3 input… in my testing.

I have also had to do the waypoints from scratch once I had changed the vector2 to vector3. but all ok so far…

I’m glad you found a solution. :slight_smile:


See also:

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

Privacy & Terms