Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Jamalakins · Jun 09, 2014 at 03:11 PM · 2d-platformerenemy aifollow player

Script for Enemy AI (2D platformer)

I'm curious of how to make an Enemy AI for a 2D platformer. I've tried many of the scripts on the forum but they all seemed to be more geared towards 3D games since when applied to my sprites they rotate in the 3rd dimension. Ideally what I would like is an enemy who patrols until the player is spotted then he follows the player until the player is in range then he attacks. If this is too much to ask then a simple follow script would still be extremely helpful. This video shows kind of how I'd like my enemy to move but I couldn't figure out how to make it work at all. Any kind of guidance would be greatly appreciated, Javascript or C# please. Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AmarokStudios · Jul 23, 2014 at 11:59 PM

Hey, I've noticed there was no answer for this question so I thought I'd give it a shot! I'm looking for the same thing... I'm almost done working on a script to do this but I need to figure out how to lock the rotation. So far, if you apply this script to an enemy, and add the "Player" tag to your player, than the enemy will follow the player but when it gets too close, it will rotate showing the other sides of the enemy (a 2D sprite... so it's not pretty lol) but for the most part, this one works.

pragma strict

private var Xpos : float; private var Ypos : float; private var max : boolean;

var Vert : boolean; var maxAmount : int; var step : float;

function Start () { Xpos = transform.position.x; Ypos = transform.position.y; }

function FixedUpdate () {

if (Vert) { if (transform.position.y >= Ypos + maxAmount) { max = true; }

     else if (transform.position.y <= Ypos)
     {
         max = false;
     }
 }
 
 else
 {
     if (transform.position.x >= Xpos + maxAmount)
     {
         max = true;
     }
     
     else if (transform.position.x <= Xpos)
     {
         max = false;
     }
 }
 
 if (Vert)
 {
     if (!max)
     {
         transform.position.y += step;
     }
     
     else
     {
         transform.position.y -= step;
     }
 }
 
 else
 {
     if (!max)
     {
         transform.position.x += step;
     }
     
     else
     {
         transform.position.x -= step;
     }
 }

}

Comment
Add comment · Show 2 · 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 XcomegaX · Jul 30, 2014 at 06:08 PM 0
Share

U can used Rigid body 2d and select Fixed Angle to lock the rotation

avatar image GabeGabe · Jan 21, 2015 at 10:29 PM 0
Share

Is this code working with the rigidbody2D fixed angel?

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

23 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

Related Questions

2d enemy ai 0 Answers

How do I make an object follow the player? 1 Answer

Mirroring Animation with Object Move 2 Answers

A Quick Question About 2D Enemy AI 0 Answers

How to create an AI follow script in Java? 2 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