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 MarSolo · May 15, 2014 at 01:23 AM · animation2danimatorenemyai

2d Top Down Enemy Control (LoZ type)

So I'm in the process of testing out certain ideas in Unity since the latest 2D update. I've always been obsessed with the play style of Zelda games and wanted to create something along that play style.

I've pretty much got the basics covered at this point thanks to plenty of tutorials I've been able to find across the web. However, I've been unable to get what I've learned and apply it to Enemy Movement, at least in regards to animation. I can get the enemies to follow me no problem and change animation direction based on my characters movement, however, if I don't have the movement of my character as a factor, the enemy stays in the "forward facing" direction.

For instance, here's my playerController:

 using UnityEngine;
 using System.Collections;
 
 public class playerController : MonoBehaviour
 {
     // The speed of the player
     public Vector2 speed = new Vector2(1, 1);
     
     // Store the movement
     private Vector2 movement;
 
     // Animation purposes
     private Animator animator;
     
     // Use this for initialization
     void Start()
     {
         animator = this.GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {    
         // Retrieve axis informatio
         var vertical = Input.GetAxis("Vertical");
         var horizontal = Input.GetAxis("Horizontal");
 
         // Movement per direction
         movement = new Vector2(speed.x * horizontal, speed.y * vertical);
         
         if (vertical > 0)
         {
             animator.SetInteger("Direction", 2);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (vertical < 0)
         {
             animator.SetInteger("Direction", 0);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (horizontal < 0)
         {
             animator.SetInteger("Direction", 1);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (horizontal > 0)
         {
             animator.SetInteger("Direction", 3);
             animator.SetFloat("Speed", 1.0f);
         }
         else
         {
             animator.SetFloat("Speed", 0.0f);
         }
 
         // Attacking
         bool attack = Input.GetButtonDown("Fire1");
         attack |= Input.GetButtonDown("Fire2");
         // Careful: For Mac users, ctrl + arrow is a bad idea
         
         if (attack)
         {
             WeaponScript weapon = GetComponent<WeaponScript>();
             if (weapon != null)
             {
                 // false because the player is not an enemy
                 weapon.Attack(false);
             }
         }
     }
 
     void FixedUpdate()
     {
         // Move the game object
         rigidbody2D.velocity = movement;
     }
 }

And here's my Enemy "AI" movement script:

 using UnityEngine;
 using System.Collections;
 
 public class AI : MonoBehaviour {
     
     private Animator animator;
     private Vector3 Enemy;
     private Vector2 EnemyDirection;
     private int Ground;
 
     public float xDir;
     public float yDir;
     private float speed = 110;
 
     void Start () 
     {
         animator = this.GetComponent<Animator>();
         Ground = 1 << 12;
     }
 
     void Update () 
     {
         Enemy = GameObject.Find ("Player").transform.position;
 
         xDir = Enemy.x - transform.position.x;
         yDir = Enemy.y - transform.position.y;
 
         EnemyDirection = new Vector2 (xDir, yDir);
 
         //line of sight
         if (!Physics2D.Raycast(transform.position, EnemyDirection, 1, Ground))
             {
                 rigidbody2D.AddForce(EnemyDirection.normalized * speed);
             }
 
         // directional control
         if (yDir > 1)
         {
             animator.SetInteger("Direction", 2);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (yDir < -1)
         {
             animator.SetInteger("Direction", 0);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (xDir < -1)
         {
             animator.SetInteger("Direction", 1);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (xDir > 1)
         {
             animator.SetInteger("Direction", 3);
             animator.SetFloat("Speed", 1.0f);
         }
         else
         {
             animator.SetFloat("Speed", 0.0f);
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         Debug.Log ("Hit " + other.gameObject);
     }
 }

Just a heads up, I'm sort of a noob when it comes to C#, and had more experience with JavaScript and C++, however, I figure now is the perfect time to learn something new right?

Comment
Add comment · Show 2
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 MikeNewall · May 15, 2014 at 01:43 AM 0
Share

Try setting the forward vector of its transform to the direction you're moving in

 transform.forward = EnemyDirection.normalized;
avatar image MarSolo · May 21, 2014 at 12:25 AM 0
Share

D'oh my apologies, things got hectic and I forgot how to find this (really new to the forums).

I made some slight changes to the code to further match the player character movement before I got a chance to read your suggestion:

 using UnityEngine;
 using System.Collections;
 
 public class AI : $$anonymous$$onoBehaviour {
 
     // Animation purposes
     private Animator animator;
 
     private Vector3 Enemy;
     private Vector2 EnemyDirection;
     private int Ground;
 
     // The speed of the enemy
     public Vector2 speed = new Vector2(1, 1);
 
     // Store the movement
     private Vector2 movement;
 
     // Use this for initialization
     void Start () 
     {
         animator = this.GetComponent<Animator>();
         Ground = 1 << 12;
     }
 
     void Update () 
     {
         Enemy = GameObject.Find ("Player").transform.position;
 
         // Retrieve axis information
         var horizontal = Enemy.x - transform.position.x;
         var vertical = Enemy.y - transform.position.y;
 
         EnemyDirection = new Vector2(horizontal, vertical);
 
         // line of sight
 //        if (!Physics2D.Raycast (transform.position, EnemyDirection, 1, Ground)) 
 //        {
 //            rigidbody2D.AddForce (EnemyDirection.normalized * speed);
 //        } 
 //        else 
 //        {
 //            animator.SetFloat ("Speed", 0.0f);
 //        }
 
         // $$anonymous$$ovement per direction
         movement = new Vector2(speed.x * horizontal, speed.y * vertical);
 
         // directional control
         if (vertical > 1)
         {
             animator.SetInteger("Direction", 2);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (vertical < -1)
         {
             animator.SetInteger("Direction", 0);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (horizontal < -1)
         {
             animator.SetInteger("Direction", 1);
             animator.SetFloat("Speed", 1.0f);
         }
         else if (horizontal > 1)
         {
             animator.SetInteger("Direction", 3);
             animator.SetFloat("Speed", 1.0f);
         }
         else
         {
             animator.SetFloat("Speed", 0.0f);
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         Debug.Log ("Hit " + other.gameObject);
     }
 }


Now I assume that the changes you suggested are possible in this as well. Where exactly would Is stick that? I commented out the AI following as that has been the trouble spot for me right now, as I'm getting a "error CS0019: Operator *' cannot be applied to operands of type UnityEngine.Vector2' and `UnityEngine.Vector2'"

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

2D Animation does not start 1 Answer

2D animation diferent right and left 0 Answers

2D Animator: how to get rid the animation "blending" 0 Answers

Delete Object After Animation (2D) 6 Answers

Sprite becomes invisible when set on animator 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