Lost in Laser Defender

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

public class EnemyPathing : MonoBehaviour
{
[SerializeField] List waypoints;
[SerializeField] float moveSpeed = 2f;
int waypointIndex = 0;
// Start is called before the first frame update
void Start()
{
transform.position = waypoints[waypointIndex].transform.position;
}

// Update is called once per frame
void Update()
{
    Move();
}

private void Move()
{
    if (waypointIndex <= waypoints.Count - 1)
    {
        var targetPosition = transform.position = waypoints[waypointIndex].transform.position;
        var movementThisFrame = moveSpeed * Time.deltaTime;
        transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame);
        if (transform.position == targetPosition)
        {
            waypointIndex++;
        }
    }
    else { Destroy(gameObject); }
}

}

That’s right. Prefabs cannot reference any game objects in the Hierarchy but don’t worry about that. Once Rick uses his path prefab for the enemy prefab, you will be able to assign your path prefab to your enemy prefab too.

The enemy only goes to waypoint (0) then does not move.

Try to replace Vector2.MoveTowards with Vector3.MoveTowards in the EnemyPathing class.

Thank you Nina for your constant trying & passion in wanting me to be able to finish this course. Let me get to the point. I tried Vector3 & I got:
1- The referenced script (Unknown) on this Behaviour is missing!
2- the enemy is no longer visible in game
3- the enemy only goes to waypoint (1) but that’s in the scene window it’s completely not in the game window.
4- I have the enemy move speed set to 001 & the enemy will fly (meaning it moves incredibly fast) from waypoint (1) to waypoint (2) but that only happens in the scene & not the game. Again, when I hit play the enemy totally disappears from the game scene.
Then obviously when I go back to Vector 2 I can see the enemy in the game but it don’t move from waypoint (0).

That’s a “good” error message because there is obviously something wrong in the editor. Go to the game object to which this message refers and remove the “missing” component. Then readd it. Does the file name match the class name inside the file? For example, is the Test class inside the Test.cs file? Sometimes, if you rename scripts, the filename and the classname could be different. That causes errors and error messages like this. Sometimes, this happens out of nowhere.

2- the enemy is no longer visible in game

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? If so, click on the invisible enemy in your Hierarchy and check its position. Maybe it moved behind the background. If it’s moving on the path, check the positions of the waypoints. The waypoints must not be behind the background or behind the camera or outside the camera viewport.

Problem #3 and #4 sound like #2 to me.

If the enemy is still in the Hierarchy, it still exists in the scene. In that case, you just have to make sure that it is in front of the camera and not hidden by any other sprites.

The error is gone now & I changed it back to Vector3. When I do Vector 3 the Enemy goes from waypoint (1) to (2) (but skips waypoint 0) at a very fast speed & this only happens on the scene not in game. Once I hit play the Enemy disappears in the game but I can see it moving swiftly from 1-2 then destroying itself when it hits 2. My background Z was originally 5 now I set it to 10 & the -10 on main camera.
So like I said if I use Vector2 the Enemy don’t move, & using Vector3 causes the enemy to disappear. When I hit play the Enemy disappears in the game 1st, then the enemy moves from 1 to 2 & destroys itself. So no the enemy is not in the hierarchy after it destroys itself.

I deleted the background to see what happens & it did not change the results.

What’s the position of the Path root game object? What’s the position of the single waypoints?

What’s the value of moveSpeed during runtime??

And what’s the purpose of the following line of code? Could you explain to me what it is supposed to do?

var targetPosition = transform.position = waypoints[waypointIndex].transform.position;

Path Position: x 13, y 4.5, z -60.
Waypoints Position: x -11, y -1, z 0.
Movespeed: .0001
The Code: I have no clue Nina. I am following Rick’s lessons & he said to use that code in a video. Truthfully, because I get stuck on the same lesson so often & for so long I really don’t understand.

One more question… isn’t a move speed of .0001 supposed to be slow?
I was trying to lower it maybe I should add more 0000000.

Thank you. The position of child game object is relative to their parent. This means that the z-position of the waypoints is actually -60. That’s behind the camera and explains why your enemy disappears during runtime.

Select the Path game object and set its z-position to 0. That should hopefully fix the problem with the invisible enemy.

A move speed of .0001 is very slow, especially, if you also multiply by Time.deltaTime.

Regarding var targetPosition = transform.position = waypoints[waypointIndex].transform.position;, this line overrides the transform.position with the waypoint position. What we actually want to do is to move the enemy towards the waypoint, thus we cannot override the enemy’s position with the waypoint position. If we just overrode the enemy’s position with the waypoint position, the enemy would teleport to the waypoint. Try to solve this problem yourself. Alternatively, you could compare your code to the Lecture Project Changes which can be found in the Resources of this lecture.

Ok I’m going to do that.
Sofar I changed the move speed in the script to [SerializeField] float moveSpeed = .00000000000000000000000000000000001f; & I was not able to get the moveSpeed in the inspector to go lower then.0001 but I’m looking into that now.
Thank you I’ll try to figure out why I’m having problems.

I made a 3rd way point because the Enemy ignores waypoint (0) so now that’s working.

Do you mean you assigned the script to the Inspector and changed the value in the code afterwards? If so, that would explain why you don’t notice any difference. Once you assigned a script to the Inspector, the Inspector controls the values at the top of your code. The values you define in your code get used only when you assign the script and when a component gets created. The Inspector uses them as the default values.

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

public class EnemyPathing : MonoBehaviour
{
[SerializeField] List waypoints;
float moveSpeed = -90000000000000000000000000000000000001f;
int waypointIndex = 0;
// Start is called before the first frame update
void Start()
{
transform.position = waypoints[waypointIndex].transform.position;
}
// Update is called once per frame
void Update()
{
if (waypointIndex <= waypoints.Count - 1)
{
var targetPosition = transform.position = waypoints[waypointIndex].transform.position;
var movementThisFrame = moveSpeed;
//there’s no delta.time.slow command I removed Rick’s time.deltatime
// var movementThisFrame = moveSpeed * Time.deltaTime; was Rick’s original Code
// Rick’s moveSpeed : [SerializeField] float moveSpeed = 2f;
// I tried to use Rick’s code but it causes errors.

        transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementThisFrame);
        if (transform.position == targetPosition)
        {
            waypointIndex++;
        }

    }
}

}

That’s what my code looks like. I been trying to figure it out on my own like you said. Lemme show you my inspector settings.

From what I see, all values have been changed, so it’s no surprise to me that things do not work as expected. In this course, we usually stick with Unity’s default values. Unless you want to keep toying around with the values to learn more about Unity, I’d suggest to reset the values. There is a cogwheel icon. Click on it.

Yes that values have been changed. If the enemy ship wasn’t moving so fast I would have not changed anything… When I was following Rick the Enemy moved too fast & I asked you to help me & we did a few things & nothing worked. Then you told me to figure it out on my own & that’s what I been trying to do.

Well ma’am as you know I’ve tried the course QnA, getting your help, reading posts on this website from people who had similar issues, googled, used github, youtube, watched videos 90-97 at least 6 times & video 95 at least 15 times, got help from friends & the facebook community, copied Rick’s code & have experimented myself & nothing gets me past video 95. I’m convinced this problem will never solve itself. Do you suggest I continue doing Laser Defender ignoring the problems I’m having until they eventually cause errors like last time? It’s possible it may never give me errors or work correctly but I have no idea what else to do. Here is my code & yes I had a lot of help to get this far.

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

public class EnemyPathing : MonoBehaviour
{
[SerializeField] List waypoints;
float moveSpeed = -90000000000000000000000000000000000001f;
// with a move speed of that low why is the enemy moving so fast?
// Rick’s code was also enemy moving too fast
// Rick’s moveSpeed : [SerializeField] float moveSpeed = 2f;
// when i used serializefield even at -10000000000 the enemy moved extremely fast ao i removed it
int waypointIndex = 0;
// Enemy ignores waypoint (0) & goes straight to 1
void Start()
{
transform.position = waypoints[waypointIndex].transform.position;
}
// Update is called once per frame
void Update()
{
if (waypointIndex <= waypoints.Count - 1)
{
var targetPosition = transform.position = waypoints[waypointIndex].transform.position;
var movementThisFrame = moveSpeed * Time.deltaTime;
//there’s no delta.time.slow command I removed Rick’s time.deltatime
// I tried to use Rick’s code but it causes errors.
transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementThisFrame);
if (transform.position == targetPosition)
{
waypointIndex++;
}
}
// Because the enemy ship moves so fast I had to remove the destroy.object function
}
}

Privacy & Terms