I’m trying to add an enemy to my platfromer game that casually walks back and forth, changing direction when touching a wall. (which i successfully implemented,). When he sees the player character, he starts suddenly stops, with an exclamation point above his head, and then starts running at a higher speed. the enemy is named “pomada”
however…
I’m trying to have it so that he can’t see anything past any walls in front of him, but I don’t know how to do that.
When you raycast, you test whether that ray intersects an object. The object you test against can be a single object or an object group. The raycast condition is true if the ray does intersect the object.
However, you want to do the opposite - check the ray doesn’t intersect the block. To achieve this, use the logical NOT operator/condition on the raycast:
That is for scenario in case you want to do something with that position
For example look at that zombie laser example
Imagine you are checking if laser did hit zombie or obstacle
If it hits obstacle and you saved X and Y position of impact into scene vars
Now you can for example spawn smoke effect at that point to make it look fancy
Where if it hits zombie you can spawn sprite object or particle emitter which would imitate blood
One way i’d go with it would be using lighting to detect your character.
Basically :
Attach a light object to your enemy sprite
Hide lights in events (or just have a “detection” layer that’s hidden)
Add light blocking behaviour to your walls/floors
When a character is in collision with light let the enemy do their thing
Adding lights into your scene will change your scenes brightness so make sure to add this event to your game if you don’t want to add any lighting later on
I’m not sure if removing the lighting layer and using the lighting on different layers increases the brightness again but saying it just in case
If the “vision” causes any problems you can keep adding invisible objects with light blocking behaviour to adjust where they can / can’t see