Lost in Laser Defender

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
}
}

Have you tried to reset the values in the settings? If so, use lower positive values in your code. Make the enemy move properly. Unfortunately, this is the first time I’ve heard about this problem, so I assume that the problem is not caused by a bug in Unity but by the settings in your Unity, by your operating system or something else on your side. If this problem is not restricted to this project and if you cannot find any solution, formatting your harddrive and reinstalling Windows and all relevant programs might be an idea.

If you have this problem in this project only, create a new project for testing purposes and try to move an object via code there. If that works, recreate the Laser Defender project. In rare cases, projects in Unity are suddenly buggy for no apparent reason.

According to the EnemyPathing class, the enemy position gets set to the position of the first waypoint. See the Start method. This means it will move from waypoint 0 to waypoint 1 instead of moving from “somewhere” to waypoint 0 first. I’m not sure if that’s why you meant by “ignoring waypoint (0)”.

I just reinatalled windows a few weeks ago I’m not doing that again I’m finally getting my pc to where I want it to be & still have work to be done. Yes I reset the values. Miss Nina throughout my years of going to school, college, taking courses etc one thing my teachers have always told me is “I have never seen that problem before”. I always run into the problems that nobody has the answer to.
Anyway what I mean by "ignoring waypoint (0) is I have 4 waypoints;
Waypoint (0) (1) (2) (3). When I hit play the enemy goes to 1-2-3 & not 0-1-2-3. The enemy does not go to the Waypoint (0) at all.

I cannot speak for your teachers but I have been supporting this course for several years and have encountered hundreds of problems in this context. For most of them, we found a solution. If I tell you, that I’ve never encountered your problem before, I mean it. And I told you that to let you know that finding a solution might take a bit longer.

I’m still wondering if I simply misunderstood your problem. :confused:

Log the position of the current target waypoint into your console.


By the way, it would be great if you could format your code to increase the readability of your code. I’ve just spotted the same issue I addressed 5 days ago: var targetPosition = transform.position = waypoints[waypointIndex].transform.position;.

That line translates to:

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

transform.position = waypoints[waypointIndex].transform.position; sets the position of your game object to the waypoint position. And if the enemy is already at the targetPosition due to this line, there will be no movement. No matter what the value of moveSpeed is, the game object gets simply teleported whenever that line gets executed.

Replace it with:

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

Remember you can also look at the lecture code changes via the link in the Resources of each lecture.


See also:

Nope you understand me totally clearly it seems. Please forgive me miss but I’m new to coding & just started with this (incredibly difficult) “Beginner” course so I have no clue what you mean when you say Log the position of the current target waypoint into your console". I downloaded the entire lecture package & have the codes side by side in VS using text editor. OK I got the link on the code I’ll do that from now on. Finally I used your code & now Enemy goes to waypoint (0) & does not move.

Finally I used your code & now Enemy goes to waypoint (0) & does not move.

If it is finally moves to Waypoint (0), that’s a step forward. Try to replace Vector2.MoveTowards with Vector3.MoveTowards in the EnemyPathing class.

I meant this:

if (transform.position == targetPosition)
{
    waypointIndex++;
    Debug.Log("Value of waypointIndex: " + waypointIndex);

    // New variable to increase the readability
    var targetPosition = waypoints[waypointIndex].transform.position;
    Debug.Log("Position of targetPosition: " + targetPosition);
}

You know what is supposed to happen in your game but you do not know what Unity does behind the scene. With these two messages, you will be able to tell when the if-block got executed, what the new value of waypointIndex and the value of the next targetPosition are. Then you are able to compare the output with your expectation. If the output does not match your expectation, you found a part of the problem and could analyse it further.

I think we’re on to something. We got a yellow error but I think it’s what you call a “good error”. So my issue is now. I hope I’m doing the format thingy correctly. The yellow error is gone now so. Assets\Scripts\EnemyPathing.cs(24,74): error CS0103: The name ‘targetPostion’ does not exist in the current context.
,…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPathing : MonoBehaviour
{
[SerializeField] List waypoints;
[SerializeField] float moveSpeed = 2f;

int waypointIndex = 0;
void Update()
{
    if (waypointIndex <= waypoints.Count - 1)
    {
        // Ricks original  var targetPosition = transform.position = waypoints[waypointIndex].transform.position;
         var targetPosition = waypoints[waypointIndex].transform.position;
            var movementThisFrame = moveSpeed * Time.deltaTime;
        transform.position = Vector2.MoveTowards(transform.position, targetPostion, movementThisFrame);
        if (transform.position == targetPosition)
            Debug.Log("Position of targetPosition: " + targetPosition);
        {
            waypointIndex++;
            Debug.Log("Value of waypointIndex: " + waypointIndex);
        }

}
// Because the enemy ship moves so fast I had to remove the destroy.object function
}
}
…,
Line 24 = }, targetPosition is on line 18.
Thank you.

If you get a message like “The name ‘targetPostion’ does not exist in the current context” and know that the variable is supposed to exist, check the spelling of the string mentioned in the message.

“Postion” looks like a typo. :wink:

Thanks. I copy & pasted Rick’s but must have done something to mess it up. I’m sorry I tried to figure out that error before asking for help. I was able to solve the : Object is too large or too far away from the origin error that popped up via google. Now the Enemy moves I guess a few centimeters when I hit play but does not go to any waypoint.



That’s how far the enemy moves now.

First of all, try to replace Vector2.MoveTowards with Vector3.MoveTowards in the EnemyPathing class. And leave it this way.

Then click on Path (0) and and make sure that the position is set to (0, 0, 0). If necessary, move the children. Set the z-position of all children to 0.

Then click on your Player and remove all waypoints from its array. Add the waypoints from your Hierarchy.

Then click the Play button.

Also check your code (#66) again. I’ve just noticed that the { } do not belong to the inner if-block because Debug.Log is outside the curly brackets. There must not be anything between the if-condition and the first curly bracket. Issues like these are very difficult to see because the code is valid, and the compiler does not help here.

Yes, vector 2 & 3 give me the same result. I go back & fourth from 2 & 3 because 2 works for some & 3 works for others.
Path (0) was already on 0,0,0, & all the children are Z on 0.
The player has no waypoints did you mean the enemy? If so do you want me to delete my waypoints & then make new ones & try again when you said to:
" remove all waypoints from its array. Add the waypoints from your Hierarchy."
Ok got you I didn’t know that thank you. So I remove everything from the if to the curly braces even if it’s in Rick’s code?
Well that produces errors. I’ll play around & see if I can figure something out because I have no clue what to do now.

Sorry, yes, I meant the enemy.

This is the part in your code that I was referring to:

    if (transform.position == targetPosition)
            Debug.Log("Position of targetPosition: " + targetPosition);
        {
            waypointIndex++;
            Debug.Log("Value of waypointIndex: " + waypointIndex);
        }

The following part is NOT part of the if-block:

        {
            waypointIndex++;
            Debug.Log("Value of waypointIndex: " + waypointIndex);
        }

Why? Because the first Debug.Log is outside the block and forms its own block. It must be inside the curly brackets.

if (condition)
{
   // part of the if-block;
   // part of the if-block;
}

if (condition)
   // part of the if-block;
{
   // not part of the if-block;
}

Can you spot the difference between the two? If so, take a very close look at your code and try to fix it.





& I was wrong it was lesson 96 so that means we’re officially on Lesson 97 & can finally label this problem as solved. Thank you for your help.

Well done! I’m glad you solved the problem. :slight_smile:

What exactly was the problem? And what did you do to solve it?


See also:

Privacy & Terms