• 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 /
  • Help Room /
avatar image
0
Question by CutCleavage · Sep 22, 2018 at 06:42 PM · playerenemydirectionfollowhow

2D How to get enemy to face the player while moving

So this is for a top-down action RPG kind of like Zelda, and I've got 4 separate animations for the enemy to face depending on which direction they're moving. I'm trying to get the enemy to follow the player if they're within range, which works, but there's a bit of a delay for the enemy to face the player, and sometimes it'll face a different direction while chasing the player, which looks pretty strange. Any ideas on how to fix this?

testController.cs

 public class testController : MonoBehaviour {
 public Transform Player;

 public float ChaseSpeed = 2f;

 public float Range = 4f;

 private float CurrentSpeed;

 private float attackTimer;
 private float attackCooldown = 0.5f;
 private bool canAttack;

 private Animator anim;


 void Start() {
     anim = GetComponent<Animator>();
 }
     void Update() {
         if (Vector3.Distance(transform.position, Player.position) <= Range)
         {
             if (Vector3.Distance(transform.position, Player.position) <= 1f)
             {
                 anim.SetBool("isWalking", false);
                 CurrentSpeed = 0f;
                 Attack ();
             }
             else
             {
                 anim.SetBool("isWalking", true);
                 anim.SetFloat("input_x", Player.localPosition.x);
                 anim.SetFloat("input_y", Player.localPosition.y);
                 CurrentSpeed = ChaseSpeed * Time.deltaTime;
                 transform.position = Vector2.MoveTowards(transform.position, Player.position, CurrentSpeed);
             }
         }
         else
         {
             anim.SetBool("isWalking", false);
             CurrentSpeed = 0f;
         }
     }
 
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

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.

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

174 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

Related Questions

Move enemy towards player if enemy is in Camera bounds? 1 Answer

Enemy Chasing player? 0 Answers

Damage indicator representing incoming damage direction with arrow in UI 2 Answers

[Solved] I can't call Method from Referenced Script 1 Answer

problem ghosting camera with 2 objets 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges