• 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 geoff_B · Dec 24, 2015 at 08:54 AM · animation2dtopdownenemy aivideo game

2D Top Down 4-directional RPG: Enemy Animation / Follow (beginner)

Hey, I'm very new to Unity and C# and I've been watching youtube videos with the goal to create a Zelda type videogame. I wanted to get enemy animations down today but I feel the need to ask guidance.

This is the C# script for my enemy AI:

 Rigidbody2D rbody;
 Animator anim;
 public Transform Player;
 public float ChaseSpeed = 5f;
 public float Range = 5f;
 float CurrentSpeed;

 void Start()
 {        rbody = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
 }

 void Update()
 {
     Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
     if (movement_vector != Vector2.zero)
     {
         anim.SetBool("is_walking", true);
         anim.SetFloat("velocity_x", movement_vector.x);
         anim.SetFloat("velocity_y", movement_vector.y);
     }
     else {
         anim.SetBool("is_walking", false);
     }

     if (Vector3.Distance(transform.position, Player.position) <= Range)
     {
         CurrentSpeed = ChaseSpeed * Time.deltaTime; 
         transform.position = Vector3.MoveTowards(transform.position, Player.position, CurrentSpeed); 
     }
 }

I'm assigning animations via a blend tree with multiple animations in them, identical to this video: https://youtu.be/XZDjkQ8wEd0?t=1267. I want the enemy to follow the player and change animations depending on which direction it's going towards (north,west,south,east), i used velocity_x and velocity_y as my animation parameters. I know a large error in my animator-code is that I refer to "input" here:

  Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

This basically makes the AI follow the player but also react to my inputs, which is not supposed to happen. So my question is, what do I replace "input" with? Is this at all going to work or do I need to change my approach completely?

I'm just trying to understand what types of options I have utilizing my code and how I can find the correct code-keywords to do what I want. I want to learn C# through unity, are there any noticable 2D Top down tutorial/books people would recommend me?

Any help is really appreciated!

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 UoUo · Apr 22, 2016 at 10:25 AM 0
Share

I feel you bro, I'm in the same position... I don´t know how to detect the "Direction/Axis" in wich the enemy is moving. Have you solved the problem by the way??

avatar image Adolf_Piggler UoUo · Nov 08, 2020 at 02:18 AM 0
Share

2020 and I'm still waiting for a response for this xD

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KevinW720 · May 27, 2020 at 05:51 PM

I know this is an old question but hopefully this will be useful to someone in the future.

You're correct that the solution can be fixed within the "input" part.

Instead of "Horizontal" and "Vertical", replace it with your rigidbody velocity.

 Vector2 movement_vector = new Vector2(rbody.velocity.x, rb.velocity.y);


 
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 amywinehasu · Nov 17, 2020 at 03:08 PM 0
Share

Hi Kevin ive got the exact same problem, losing my mind over this please help end my suffering! public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { public Animator anim;

     public float speed = 10.0f;
     public Rigidbody2D rb;
     protected Vector2 movement;
     
 
     void Start()
     {
         rb = this.GetComponent<Rigidbody2D>();
         anim = gameObject.GetComponent<Animator>();
     }
 
     
     void Update()
     {
         movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
 
         float InputX = Input.GetAxis("Horizontal");
         float InputY = Input.GetAxis("Vertical");
 
         //if (InputX != 0 || Input !=0)
         if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
         {
             anim.SetBool("walking", true);
 
             if (InputX > 0.5f)
             {
                 anim.SetFloat("speedX", 1f);
             }
             else if (InputX < -0.5f)
             {
                 anim.SetFloat("speedX", -1f);
             }
             else if (InputX > 0 && InputX <= 0.5f)
             {
                 anim.SetFloat("speedX", 0.5f);
             }
             else if (InputX < 0 && InputX >= -0.5f)
             {
                 anim.SetFloat("speedX", -0.5f);
             }
             else
             {
                 anim.SetFloat("speedX", 0);
             }
             if (InputY > 0.5f)
             {
                 anim.SetFloat("speedY", 1f);
             }
             else if (InputY < -0.5f)
             {
                 anim.SetFloat("speedY", -1f);
             }
             else if (InputY > 0 && InputY <= 0.5f)
             {
                 anim.SetFloat("speedY", 0.5f);
             }
             else if (InputY < 0 && InputY >= -0.5f)
             {
                 anim.SetFloat("speedY", -0.5f);
             }
             else
             {
                 anim.SetFloat("speedY", 0);
             }
         }
         else
         {
             anim.SetBool("walking", false);
         }
     }
 
 
     
 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

How do I make working animations on my enemies (topdown),How can I make working animations on an AI enemy topdown (isometric) 0 Answers

How do I synchronize texture offset animation with world movement? 1 Answer

Best way to do animate soldier? 1 Answer

How do I make my enemy have animations for directions during movemvent 1 Answer

2D Animation does not start 1 Answer


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