Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-2
Question by Linkthehylian04 · Jun 24, 2017 at 01:59 PM · c#2d-platformerscripting beginnerenemy ai

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
      .....
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
8

Answer by Manpreet_96 · Jun 24, 2017 at 02:18 PM

If you are willing to pay a few bucks, then Y not... :P

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rickyinjp · Jun 24, 2017 at 02:27 PM 1
Share

@$$anonymous$$anpreet_96 This!! lol

avatar image Linkthehylian04 · Jun 24, 2017 at 06:49 PM 0
Share

This is a very mean post. If I had 100 reputation, I would downvote this.

avatar image Rickyinjp Linkthehylian04 · Jun 25, 2017 at 02:27 AM 4
Share

@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.

avatar image Manpreet_96 · Jun 25, 2017 at 08:36 AM 2
Share

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. :)

avatar image
0

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");} }
              
          
     
     
 
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linkthehylian04 · Jun 24, 2017 at 06:50 PM 0
Share

Please don't support $$anonymous$$anpreet_96.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

341 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges