- Home /
Can someone help me make an Enemy AI script?
I'm making a 2D Platformer. I'd like to implement enemies that will attack the player. Can someone help me make a script for this?
Answer by Rickyinjp · Jun 24, 2017 at 02:19 PM
I can try since I'm in the process of making the same for a 3D platformer.
First, what type of enemy is it? Are we thinking about something simple like a Goomba?
If that's the case, have the enemy walk a certain direction. The enemy has a collider around its body. It has a Raycast going from its center to its front a distance d. If the player hits this Ray, you can make the enemy attack it (shoot, or charge at the player, etc.). Check this out:
bool SeesPlayer()
{
RaycastHit2D hitInfo = Physics2D.Raycast(position, facingDirection, d);
if (hitInfo.collider != null)
hitInfo.transform.gameObject.layer. == LayerMask.NameToLayer("Player"))
return true;
else
return false;
}
This is very simple but hey, simple game mechanics are usually among the most fun!
Edit: I edited the script because the original one I wrote was for 3D. Sorry it was late when I answered. The principle is the same. The distance d is a float representing the length of the ray, i.e., the distance the enemy is able to see the enemy. position is the enemy's position and facingDirection is a Vector2D representing the direction that the enemy's facing.
Later in Update you can do this:
void Update()
{
....
if (seesPlayer)
Attack();
.....
}
Answer by Manpreet_96 · Jun 24, 2017 at 02:18 PM
If you are willing to pay a few bucks, then Y not... :P
This is a very mean post. If I had 100 reputation, I would downvote this.
@Linkthehylian04 His intentions aren't bad. I think that what he's getting at is that you have to try to think about the problem and try to solve it before you ask the question ins$$anonymous$$d of saying something like "help me write this script." In my answer I was trying to point you in the right direction. Ask, what do you want the enemy to do then try to figure out a way to make it do that.
Haha @Rickyinjp :D And hey buddy @Linkthehylian04 cheer up a little. You are game developer. Have some fun , enjoy it. Don't take a little criticism to heart. Honestly, I didn't had bad intentions , all wanted was to pump you up a bit so that you could shake a leg, just want you to come up with a solution yourself. Common, read your question, there is no detail on what you need help with. what kind of enemies or whatsoever. So, it looks like you haven't done anything about it yet and in the simplest terms wanted someone to cook the script up for you. Now if you feel offended go on, down vote me but if you act upon my words you will do better off. :)
Answer by toddisarockstar · Jun 24, 2017 at 03:34 PM
Manpreet_96's answer would pry be better than this one. but here is what you get for free:)
//attach this script to enemy object then
//drag and drop player object into emply slot in the inspector on this script
public GameObject player:
void Update() {
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, 100);
if(Vector3.Distance(player.transform.position, transform.position)<.2f){print("you died");} }
Your answer
Follow this Question
Related Questions
How do I make it so it spawns 1 clone instead of 2? 2 Answers
How do I make the enemy script/AI stop following me when It is in view of the player camera? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2d enemy ai 0 Answers