Enemey sight/ai

I want it so that the enemy can see the player but not through these walls that i made, can someone help?

1 Like

oh yeah also the bullets are messed up for some reason

1 Like

Do you want me to walk you through how the code is done, or just make a bundle and post it immediately?

either works, a bundle if good

1 Like

Ok, first we need to fix the gun.


This is your gun object right now. If you want the gun to shoot towards the cursor, just connect the rot output on Point At to the Angle input on the Emit behavior.

Now that that’s fixed, let’s get to the enemy.
First, set the player’s X and Y in two globals:

{"data":{"behavior":{"v":"2","nodes":[{"inputCount":0,"outputCount":1,"name":"Always","behaviorType":"logic.triggers.Always","x":-540,"y":488,"group":"","id":"3ac40be764ed694e"},{"inputCount":1,"outputCount":1,"name":"Extractor","behaviorType":"logic.properties.Extractor","x":-360,"y":416,"group":"","id":"3ac41e098edf3145","targetId":0,"prop":"x","version":2},{"inputCount":1,"outputCount":1,"name":"Extractor","behaviorType":"logic.properties.Extractor","x":-360,"y":512,"group":"","id":"3ac41ea5cb0b7243","targetId":0,"prop":"y","version":2},{"inputCount":3,"outputCount":1,"name":"Global","behaviorType":"logic.logic.Global","x":-180,"y":416,"group":"","id":"3ac441406c82724c","tag":"Player X:Number","version":2,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Global","behaviorType":"logic.logic.Global","x":-180,"y":520,"group":"","id":"3ac441d7a0b3ae4e","tag":"Player Y:Number","version":2,"dataType":2}],"links":[{"input_id":"3ac41e098edf3145i0","output_id":"3ac40be764ed694eo0"},{"input_id":"3ac41ea5cb0b7243i0","output_id":"3ac40be764ed694eo0"},{"input_id":"3ac41e098edf3145i0","output_id":"3ac40be764ed694eo0"},{"input_id":"3ac441406c82724ci0","output_id":"3ac41e098edf3145o0"},{"input_id":"3ac41ea5cb0b7243i0","output_id":"3ac40be764ed694eo0"},{"input_id":"3ac441d7a0b3ae4ei0","output_id":"3ac41ea5cb0b7243o0"},{"input_id":"3ac441406c82724ci0","output_id":"3ac41e098edf3145o0"},{"input_id":"3ac441d7a0b3ae4ei0","output_id":"3ac41ea5cb0b7243o0"}]}}}

Put this code in the player object.

Put this in the enemy object:

{"data":{"behavior":{"v":"2","nodes":[{"inputCount":0,"outputCount":0,"name":"Follow","behaviorType":"logic.NodeGroup","x":-180,"y":424,"group":"","id":"3ac13b4688e48844","isMenuItem":false},{"inputCount":0,"outputCount":1,"name":"Always","behaviorType":"logic.triggers.Always","x":-180,"y":424,"group":"3ac13b4688e48844","id":"3ab8099968ca134f"},{"inputCount":2,"outputCount":3,"name":"Proximity","behaviorType":"logic.components.Prox2","x":0,"y":432,"group":"3ac13b4688e48844","id":"3ab87432b4d76b4a","targetEntityId":0,"threshold":90,"targetClassId":18035393,"nearestOnly":false,"firstObject":true,"allObjects":false,"contains":false,"shape":0,"oX":0,"oY":0,"pin":false},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":180,"y":464,"group":"3ac13b4688e48844","id":"3ab8a4316255e745","_startVal":0,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":360,"y":464,"group":"3ac13b4688e48844","id":"3ab968d2b2a4c248","_startVal":0,"dataType":2},{"inputCount":3,"outputCount":2,"name":"RayCast","behaviorType":"logic.components.RayCast","x":180,"y":568,"group":"3ac13b4688e48844","id":"3ab97fb82c3dd448","targetClassId":18057383,"direction":0,"length":50,"earlyOut":false,"oX":0,"oY":0,"pin":false},{"inputCount":3,"outputCount":3,"name":"Animation","behaviorType":"logic.properties.Animation","x":720,"y":584,"group":"3ac13b4688e48844","id":"3aba02ef38a6114b","animationName":"Walk","priority":0},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":540,"y":472,"group":"3ac13b4688e48844","id":"3abaa3e806696c47","startVal":1,"tag":"","roundMode":1},{"inputCount":3,"outputCount":3,"name":"Velocity","behaviorType":"logic.properties.Physics","x":720,"y":448,"group":"3ac13b4688e48844","id":"3abac0e992a8fe4e"},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":540,"y":376,"group":"3ac13b4688e48844","id":"3abb7800e0403d4f","startVal":0,"roundMode":1},{"inputCount":3,"outputCount":1,"name":"Global","behaviorType":"logic.logic.Global","x":180,"y":656,"group":"3ac13b4688e48844","id":"3abd954794dffd4a","tag":"Player X:Number","version":2,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Global","behaviorType":"logic.logic.Global","x":180,"y":736,"group":"3ac13b4688e48844","id":"3abd95e28c21814a","tag":"Player Y:Number","version":2,"dataType":2},{"inputCount":2,"outputCount":1,"name":"PointAt","behaviorType":"logic.components.PointAt","x":540,"y":696,"group":"3ac13b4688e48844","id":"3abdc2fc66e08b48","skipRot":false},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":360,"y":632,"group":"3ac13b4688e48844","id":"3abf05884678284f","_startVal":0,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":360,"y":720,"group":"3ac13b4688e48844","id":"3abf064f3bf1e043","_startVal":0,"dataType":2}],"links":[{"input_id":"3ab87432b4d76b4ai1","output_id":"3ab8099968ca134fo0"},{"input_id":"3ab97fb82c3dd448i2","output_id":"3ab8099968ca134fo0"},{"input_id":"3ab8a4316255e745i1","output_id":"3ab87432b4d76b4ao0"},{"input_id":"3ab8a4316255e745i2","output_id":"3ab87432b4d76b4ao0"},{"input_id":"3ab8a4316255e745i1","output_id":"3ab87432b4d76b4ao1"},{"input_id":"3ab8a4316255e745i2","output_id":"3ab87432b4d76b4ao1"},{"input_id":"3abb7800e0403d4fi1","output_id":"3ab87432b4d76b4ao2"},{"input_id":"3ab8a4316255e745i0","output_id":"3ab87432b4d76b4ao2"},{"input_id":"3ab968d2b2a4c248i2","output_id":"3ab8a4316255e745o0"},{"input_id":"3abaa3e806696c47i1","output_id":"3ab968d2b2a4c248o0"},{"input_id":"3ab968d2b2a4c248i0","output_id":"3ab97fb82c3dd448o0"},{"input_id":"3abf05884678284fi0","output_id":"3ab97fb82c3dd448o0"},{"input_id":"3abf064f3bf1e043i0","output_id":"3ab97fb82c3dd448o0"},{"input_id":"3ab968d2b2a4c248i1","output_id":"3ab97fb82c3dd448o1"},{"input_id":"3abf05884678284fi1","output_id":"3ab97fb82c3dd448o1"},{"input_id":"3abf064f3bf1e043i1","output_id":"3ab97fb82c3dd448o1"},{"input_id":"3abac0e992a8fe4ei2","output_id":"3abaa3e806696c47o0"},{"input_id":"3aba02ef38a6114bi0","output_id":"3abaa3e806696c47o0"},{"input_id":"3abac0e992a8fe4ei2","output_id":"3abb7800e0403d4fo0"},{"input_id":"3aba02ef38a6114bi1","output_id":"3abb7800e0403d4fo0"},{"input_id":"3abf05884678284fi2","output_id":"3abd954794dffd4ao0"},{"input_id":"3abf064f3bf1e043i2","output_id":"3abd95e28c21814ao0"},{"input_id":"3abdc2fc66e08b48i0","output_id":"3abf05884678284fo0"},{"input_id":"3abdc2fc66e08b48i1","output_id":"3abf064f3bf1e043o0"}]}}}

I’m pretty sure it should work. I tried testing it, but there seemed to be a bug. I’ll test it another time, after reloading the page.

@WilliamWatsn

thanks with that lol

1 Like

oh i didnt see this, ill work on this now

1 Like

I didn’t add this in the bundle, but press Rotate Scale With Sprite in the raycast behavior:


I tested it, it works.

ok but the guy just keeps spinning? is that just on my end?

1 Like

?

Did you import the code?

Try reloading the page, it works for me.

it was when he hit a wall it would just spin and spin but i reloaded and it works now

1 Like

Ok, do you need help with anything else?

i think its good, but itd be nice if he didnt completly spin around but idk if you can fix that and if when they dont see the enemy they go back to a spot

1 Like

and they didnt always look at the player but i guess thatd be hard to do

You only want him to flip, instead of spinning?

I’ll get right to it.

yeah, thanks bro, pratically coding my game for me lol

1 Like

Does that bother you? Understanding the code is what counts.

no, i mean i understand the code once you put it in i can see whats going on

1 Like

Put the Raycast to 10000, just for safety.

Also im going to try to make a “when this close” the enemy will stop and try to shoot them, but maybe ill do that once youre done with this stuff

1 Like