• 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 ZujinD · Apr 21, 2015 at 05:51 AM · movementaitopdowntop-downtop down

Unity2D AI for Top Down Zelda-Type movement

I've been trying to wrap my head around enemy movement for a couple weeks now, and I'm not quite sure I understand very much at all.

I was using some code from a tutorial I found at some point. Here it is

     void Update()
     {
         transform.LookAt(target.position);
         transform.Rotate(new Vector3(0,-90,0),Space.Self);    
         if (Vector3.Distance(transform.position,target.position)>1f)
         {
             transform.Translate(new Vector3(speed * Time.deltaTime,0,0) );
         }        
     }

This causes the enemy to chase the player down, but causes the sprite to rotate on all axis.

What I want, and can't seem to figure out, is for enemies to only have left-looking and right-looking states, and still move towards the player in all directions.

So there would be no actual rotation, just a flipping of the sprite direction.

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
1
Best Answer

Answer by DwaynePritchett · Apr 21, 2015 at 06:04 AM

If you're wanting left/right only. You won't want a transform.LookAt. That will use all axes. Try a simple if check, to see if the player is to the left or right of the enemy. Do this on the "Enemy Movement/AI" script. Basically, if(target.position.x < transform.position.x) mySprite = facing left; else my Sprite = facing right.

On another note, you might want to make a generic velocity by having your "monster" track his own position. Example... //Enemy Movement Script

 Vector3 currentPosition, lastPosition;
 
 Start(){
 currentPosition = transform.position;
 lastPosition = currentPosition;
 }
 
 Update(){
      if(currentPosition.x > lastPosition.x){
       //We are moving to the right
      //set sprite to rightFacingSprite
      }
      if(currentPosition.x < lastPosition.x){
       //We are moving to the left
      //set sprite to leftFacingSprite
      }

      //Update the new positions
      lastPosition = currentPosition;
      currentPosition = transform.position;
 }


This way, even if your monster hasn't "sensed/angroed" the "hero", he will still look back and forth as he moves along his default path. (Assuming, he has a default path.)

Hope this helps. NOTE* That code isn't meant to just work out of the box, but rather give you an idea of what needs done.

-Dwayne Pritchett

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 ZujinD · Apr 21, 2015 at 11:01 PM 0
Share

This is what I cam up with based on your input and a little research into Vector2s and it works how I pictured it, thanks!

 using UnityEngine;
 using System.Collections;
 
 public class AI : $$anonymous$$onoBehaviour 
 {
     
     public Transform target;//set target from inspector ins$$anonymous$$d of looking in Update
     Quaternion enemyRotation;
     Vector2 playerPos, enemyPos;
 
 
     void Start()
     {
         enemyRotation = this.transform.localRotation;
     }
 
     void Update()
     {
         playerPos = new Vector2(target.localPosition.x,target.localPosition.y);//player position 
         enemyPos = new Vector2 (this.transform.localPosition.x, this.transform.localPosition.y);//enemy position
         if (Vector3.Distance (transform.transform.position, target.transform.position) > 1.6)//move towards if not close by 
         {
             transform.position = Vector2.$$anonymous$$oveTowards (enemyPos, playerPos, 2 * Time.deltaTime);
         }
         if (Vector3.Distance (transform.transform.position, target.transform.position) < 1.55)//move away if too close 
         {
             transform.position = Vector2.$$anonymous$$oveTowards (enemyPos, playerPos, -1 * Time.deltaTime);
         }
 
         if (target.position.x > transform.position.x)//rotates enemy to the right if player is to the right  
         {
             enemyRotation.x = 180;
             transform.localRotation = enemyRotation;
         }
         if (target.position.x < transform.position.x)//rotates enemy to the left if player is to the left 
         {
             enemyRotation.x = 0;
             transform.localRotation = enemyRotation;
         }
     }
 }
avatar image DwaynePritchett · Apr 22, 2015 at 02:20 AM 1
Share

Glad it helped!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Top-Down Shooter Motion 2 Answers

My top-down movement is really jittery, any idea how to fix? 1 Answer

2D rigidbody top down movement problem 3 Answers

Top-down movement in 2D 1 Answer

Enemy strafing AI Top Down 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