Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Ai collision / enemy collision with walls
- TheGeniusCoder
-
Scratcher
100+ posts
Ai collision / enemy collision with walls
I am looking for a collision scrpit to detect when an enemy is touching a wall and to move them out of the wall, I would the ai to be able to move in coincidance with this script.
say for example if they are moving they should move away from the wall as to not get stuck and not clip within, but still continue to chase the player.
What I am doing as of now is
The position of the enemy is tracked before movement is made and then the collision is check and if it ends up touching a wall then it is moved back to the previous position, while this does work it can leave big gaps between the enemy and wall.
while I do not require pixel perfect collision for the enemies I do need the space between wall and enemy to be as minimal as possible.
I also have the enemy moving while the player is not within their line of sight but since I am running this script without refresh in a custom block it does not show properly. However I do have a solution for this,
If anyone seeing this does have an idea please post an example using scratch blocks below. I require assistance asap.
say for example if they are moving they should move away from the wall as to not get stuck and not clip within, but still continue to chase the player.
What I am doing as of now is
if <touching [ wall] ?> then
go to x: (item (1) of [enemy x pos] :: list) y: ((item (1) of [enemy y pos] :: list))
end
The position of the enemy is tracked before movement is made and then the collision is check and if it ends up touching a wall then it is moved back to the previous position, while this does work it can leave big gaps between the enemy and wall.
while I do not require pixel perfect collision for the enemies I do need the space between wall and enemy to be as minimal as possible.
I also have the enemy moving while the player is not within their line of sight but since I am running this script without refresh in a custom block it does not show properly. However I do have a solution for this,
If anyone seeing this does have an idea please post an example using scratch blocks below. I require assistance asap.
- JackDude_e
-
Scratcher
50 posts
Ai collision / enemy collision with walls
Can u add a link to the game so I can see some additional scripts that may be causing said problem?
- Cutie_Pooge
-
Scratcher
500+ posts
Ai collision / enemy collision with walls
This question seems similar to another one, you probably want pathfinding to the player
- SteelDragon3000
-
Scratcher
2 posts
Ai collision / enemy collision with walls
@TheGeniusCoder I hope this is what you are looking for!
https://scratch.mit.edu/projects/416081112/
I'm sorry, its kind of too long and complicated to put it into Scratch blocks so I made a project on it.
https://scratch.mit.edu/projects/416081112/
I'm sorry, its kind of too long and complicated to put it into Scratch blocks so I made a project on it.
- TheGeniusCoder
-
Scratcher
100+ posts
Ai collision / enemy collision with walls
This question seems similar to another one, you probably want pathfinding to the playerI'm using a line of sight code from FUNUT (now professor blue on youtube) so the normal way I would sense collision won't work because the line of sight deals with pointing towards a player which changes the costumes hitbox I think I found a work round for this but just incase, what project featured a path finder or video tutorial explains it?
- Joshia_T
-
Scratcher
500+ posts
Ai collision / enemy collision with walls
I am looking for a collision scrpit to detect when an enemy is touching a wall and to move them out of the wall, I would the ai to be able to move in coincidance with this script.Technically, avoiding pathfinding, you are currently in probably the best method (Personally). So I'm assuming the line of sight you are talking about works through walls, if it were me, I would just change the AI to turn whenever it finds a wall and move a couple of steps before going back to a “chase” state. This is the dumbest an AI can be but it works better than being stuck (kind of like a Roomba maybe? Thingy that bumps into a wall and turns over).
say for example if they are moving they should move away from the wall as to not get stuck and not clip within, but still continue to chase the player.
What I am doing as of now isif <touching [ wall] ?> then
go to x: (item (1) of [enemy x pos] :: list) y: ((item (1) of [enemy y pos] :: list))
end
The position of the enemy is tracked before movement is made and then the collision is check and if it ends up touching a wall then it is moved back to the previous position, while this does work it can leave big gaps between the enemy and wall.
while I do not require pixel perfect collision for the enemies I do need the space between wall and enemy to be as minimal as possible.
I also have the enemy moving while the player is not within their line of sight but since I am running this script without refresh in a custom block it does not show properly. However I do have a solution for this,
If anyone seeing this does have an idea please post an example using scratch blocks below. I require assistance asap.
- Cutie_Pooge
-
Scratcher
500+ posts
Ai collision / enemy collision with walls
It maybe possible to store a player's positions and then the A.I will just go where it last saw the player. If the player didn't get stuck the A.I won't get stuck. You can also skip a few positions if you use the line of sight and detect if there is any wall between some positions.
- TheGeniusCoder
-
Scratcher
100+ posts
Ai collision / enemy collision with walls
So i figured it out… love sudden breakthroughs.
thank you all for helping me out really appreciate the scratch community.
I simplified the collision code for the player to remove it being pixel perfect because as I said it isn't necessary for the enemies. This collision code gave me many problems in the past week but now I have a pixel perfect collision engine, and refreshed my knowledge of collisions, and some new ideas about ai within scratch.
As for Cutie_Pooge thank you for helping me with all my collision issues you allowed me to figure out how to set up a pixel perfect collision system, even if I wasn't directly given the code the discussion between everyone allowed me to think differently enough to get it on my own.
Although I don't plan on releasing the project on scratch perhaps I will release the pixel perfect player movement script.
thank you all for helping me out really appreciate the scratch community.
I simplified the collision code for the player to remove it being pixel perfect because as I said it isn't necessary for the enemies. This collision code gave me many problems in the past week but now I have a pixel perfect collision engine, and refreshed my knowledge of collisions, and some new ideas about ai within scratch.
It maybe possible to store a player's positions and then the A.I will just go where it last saw the player. If the player didn't get stuck the A.I won't get stuck. You can also skip a few positions if you use the line of sight and detect if there is any wall between some positions.
Thank you both, To Joshia_T yeah I agree path finding is better for grid based movement if it were to be used for my game it would just increase lag and be unnecasary, adding line of sight and then different states for the enemy along with a collision system for it allows it to roam freely, and find the player and move around the level without having all the complications with path finding (although with the right pathfinder it could work). Also yeah It's pretty much a roomba xD (the current enemy is less roomba like however in a different game the enemy moved around exactly like one).I am looking for a collision scrpit to detect when an enemy is touching a wall and to move them out of the wall, I would the ai to be able to move in coincidance with this script.Technically, avoiding pathfinding, you are currently in probably the best method (Personally). So I'm assuming the line of sight you are talking about works through walls, if it were me, I would just change the AI to turn whenever it finds a wall and move a couple of steps before going back to a “chase” state. This is the dumbest an AI can be but it works better than being stuck (kind of like a Roomba maybe? Thingy that bumps into a wall and turns over).
say for example if they are moving they should move away from the wall as to not get stuck and not clip within, but still continue to chase the player.
What I am doing as of now isif <touching [ wall] ?> then
go to x: (item (1) of [enemy x pos] :: list) y: ((item (1) of [enemy y pos] :: list))
end
The position of the enemy is tracked before movement is made and then the collision is check and if it ends up touching a wall then it is moved back to the previous position, while this does work it can leave big gaps between the enemy and wall.
while I do not require pixel perfect collision for the enemies I do need the space between wall and enemy to be as minimal as possible.
I also have the enemy moving while the player is not within their line of sight but since I am running this script without refresh in a custom block it does not show properly. However I do have a solution for this,
If anyone seeing this does have an idea please post an example using scratch blocks below. I require assistance asap.
As for Cutie_Pooge thank you for helping me with all my collision issues you allowed me to figure out how to set up a pixel perfect collision system, even if I wasn't directly given the code the discussion between everyone allowed me to think differently enough to get it on my own.
Although I don't plan on releasing the project on scratch perhaps I will release the pixel perfect player movement script.
- Username321go
-
Scratcher
2 posts
Ai collision / enemy collision with walls
Im making a gaem where you get chased by this person irl. Can you guys help me with making enemy ai
- bobcat1819
-
Scratcher
3 posts
Ai collision / enemy collision with walls
uhhh so i need help on making enemy wall collision and its not working idk why
- TheGeniusCoder
-
Scratcher
100+ posts
Ai collision / enemy collision with walls
uhhh so i need help on making enemy wall collision and its not working idk whyyou should provide your project link so that people can view the code and help you
- Whopper_Dog
-
Scratcher
80 posts
Ai collision / enemy collision with walls
im looking for code that stops my enemy from going into my player and collides instead also i need a knock back sytem for damage for both player and enemy
- kojaty
-
Scratcher
1 post
Ai collision / enemy collision with walls
thanks broskis, this is going to be helpful to me, have a nice tea 

- Discussion Forums
- » Help with Scripts
-
» Ai collision / enemy collision with walls
