#Question on Pac-Man Clone Based on Guide Playlist

1 messages · Page 1 of 1 (latest)

feral oriole
#

Hello there, folks. I apologize in advance for how loaded of a question this is, but if someone could lend a hand, I would be deeply appreciative.

For a university assignment, I was tasked to program a Pac-Man clone in Unity, and I have been told I am allowed to use a guide on this. So far, most of the coding has gone well, but I am running into an issue on video 11 in the guide playlist. There are nodes that the ghosts are supposed to move to when the boolean value testRespawn is checked true, which the guide showcases around the 11:24 timestamp, but this sequence never happens for my project.

I have attempted to see if the function where this occurs is called correctly by using Debug Logs, but those do not print in the console and I'm not sure why. My code is almost 1 to 1 with the guide's scripts, the sole differences being the resolution of the game, tile sizes (as per the art team's specifications) and a few variable names, all of which have seemingly been accounted for in my scripting.

Unfortunately, the source code in the video has not been posted. However, I do have links to my code on Pastebin, the playlist for the guide I am following, the specific video I have reached in the playlist and a video of the bug in the Unity Editor provided below.

Again, I apologize for how much there is to sift through in this question, but if anyone has advice, I would be very thankful to hear it! 👍

Pastebin Code Links:

YouTube Guide Playlist Link:
https://youtube.com/playlist?list=PLHrN7HL-00e8HECYZFE-9i9Qf_SRqWxZh&si=mxtC1JmzxwPSEBtP

YouTube Guide Video (11th in Playlist Above):
https://youtu.be/QpBoIxxkrw4?feature=shared

Unity Version: 2021.3.22f1

Welcome to part 11 of our Unity 2D tutorial where we are building Pacman!

Assets:
https://drive.google.com/drive/folders/1RdJQ3S0F-iW3wkb2yQbfxGlVvTMS6Gik?usp=sharing

Thanks for watching, if you enjoyed, don't forget to like and subscribe for anything Brilliant!

Where else can you find us?
https://www.brilliantlabs.ca/
https://twitter.com/br...

▶ Play video
craggy crystal
#

It's a lot of code and video to check, so I'm not gonna go through all of it, but I might be able to direct you in the right direction to debug this issue.

feral oriole
#

Gotcha- the debug log is in line 113 of the EnemyController script (the 1st Pastebin link)

craggy crystal
# feral oriole Gotcha- the debug log is in line 113 of the EnemyController script (the 1st Past...

One possible case is that you're comparing floats.
Comparing floats is very unreliable due to floating point error and even more so for positions of objects that don't move in discreet units.

transform.position.x == ghostNodeStart.transform.position.x && transform.position.y == ghostNodeStart.transform.position.y

Instead of the code above I'd suggest making a distance check with Vector3.Distance or check if the magnitude of the difference vector is small enough.

P.s. is that really how they do it in the tutorial?

feral oriole
#

I've tried following the guide to the letter so far, yeah. It seems pretty unreliable at this rate, but everything else so far has been working. Plus, I'm already 11 videos in and switching to a different tutorial now would likely jeopardize my whole group's grade

craggy crystal
#

Looking at the code, there's a lot of crazy stuff... It feels like the guy just jumped out of python and decided to develop a game in unity.😅

feral oriole
#

For reference, this group assignment is fully due next Sunday, and what I have so far is for a prototype build; I have enough content for the prototype build, but with half of the playlist left to go, I'd rather get through it as quickly as possible

craggy crystal
#

Assuming floating point error is not an issue here, then the enemy doesn't reach the specified node. That or it's not in the respawning state at that point.

#

I've no clue what the logic behind that code is, but the cause of the issue is simple. These conditions are not true when you expect them to be true:

else if (ghostNodeState == GhostNodeStatesEnum.respawning)
        {
            string direction = "";
 
            //Ghost has reached start node- move to center node (TO DO: THIS DOESN'T WORK FOR SOME REASON, ASK CALL)
            if (transform.position.x == ghostNodeStart.transform.position.x && transform.position.y == ghostNodeStart.transform.position.y)
#

Or the method that they're in is not being called when it should.

#

If you can isolate the timing of when that code is supposed to run, you could step through the code with the debugger to see what's going on. Alternatively, place more logs to see the internal state of the enemy.

feral oriole
#

I see-- just for reference, what would the Vector3.Distance comparison look like if I were to try that?

craggy crystal
#

You can check the documentation on how to use the method. It has an example too.

You'd use the enemy position and the node position as the arguments and if the returned distance is less than some very small number, you'd consider that the positions match.

#

That being said, I'm not 100% sure that's the issue.
It's really hard to say what's going on with that spaghetti code.

feral oriole
#

I'll still test it out! Thank you so much! I'll try and steer clear of this YouTube channel when it comes to guides from this point forward, too 👌

feral oriole
feral oriole
# craggy crystal I've no clue what the logic behind that code is, but the cause of the issue is s...

As for the bug, I've tried implementing a new debug log to pinpoint the issue. After inserting one at line 108 (above the declaration of the string variable "direction" in this snippet), I can confirm that the debug log is called for when the ghostNodeState is set to "respawning," but not for when Blinky's position matches the position of the starting ghost node. This even applied to when I ran the project in the editor and manually set Blinky's coordinates to match the starting ghost node's, which should have recognized the if statement as having its conditions met.

#

For the Vector3.Distance method, I tried making a rendition of my current EnemyController script with it, but I did not see any new results. I'm not sure if I implemented the method incorrectly or if there is another script clashing with it, but I'll add a Pastebin link to this version of the script as well:

EnemyController (with Vector3.Distance Method): https://pastebin.com/0iT4d0Rf

craggy crystal
feral oriole
#

Blinky is the name of the ghost

craggy crystal
feral oriole
#

Blinky is in the respawning state, but once the parameters of the respawning state are met, Blinky is supposed to go to the node in the center of the ghosts’ home

#

The issue is that for some reason, these parameters are never recognized, so instead of going down into the center ghost node, it just loops around a stretch of the maze

craggy crystal
#

Is it in the respawning state when it gets to the center of the node or not? That's what I'm asking.

#

So, it doesn't even go to the respawn node..?

#

Ok, the video loaded now,so I see it myself.

#

Where is the logic that should make it go to the respawn node?

feral oriole
#

In the EnemyController script, lines 88 through 94 is the logic that sets Blinky’s state to respawning when the Boolean is checked to be true in the inspector. Lines 106 through 150 in the EnemyController script contain the logic for sending Blinky to the starting ghost node, then the center ghost node

#

Lines 88-94:

#

Lines 106-150:

#

In the 2nd screenshot, the 1st debug log is printed to the console, but the 2nd is not, which would mean the logic to move Blinky down to the center node is never detected

#

This holds true even when I set his coordinates in the inspector to match the start node's coordinates exactly

craggy crystal
#

You want to make it clear what state the ghost is in and why when it reached that point where it should go down, bit doesn't.

craggy crystal
feral oriole
#

Okay, I'll try that right away

craggy crystal
#

Is there even a node there?

feral oriole
#

The isGhostStartingNode boolean returns true, so it should be

craggy crystal
#

In your movement controller, this code, starting from line 69 looks very suspicious. Check if it enters there when it's supposed to go down.

            //Otherwise, find next node
            else
            {
                //If we are not a respawning ghost and we have reached the start node, stop us if we try to move down
                if (currentNodeController.isGhostStartingNode && direction == "down"
                    && (!isGhost || GetComponent<EnemyController>().ghostNodeState != EnemyController.GhostNodeStatesEnum.respawning)) 
                {
                    direction = lastMovingDirection;
                }
#

Or rather before that even

#

Check if the starting node is ever assigned to the currentNode field

#

Basically debug every time you assign the the currentNode.

feral oriole
#

So to debug this correctly, would I place the log at the end of Update like this?

craggy crystal
#

That's why people usually encapsulate such important logic in a separate method, like SetCurrentNode and put debugs inside of it.

#

If you were to use the debugger, you could add a breakpoint on value change.

feral oriole
#

Okay, it's as you suspected: the starting ghost node does not appear to be detected as a node that Blinky goes to

#

If it was, it would return the name "Start"

#

But it seems to have skipped past it

#

It is still a node, just not one Blinky stops at

craggy crystal
#

Great. Now you know what part of code to look at. Investigate the logic. Why does it not get the starting node assigned? Compare to the tutorial code.