• 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 danielg8 · Jan 20, 2019 at 12:18 AM · rotationai2d-platformerfollow player

How do you make enemies rotate to your position in 2d

Hi I am trying to make it so enemies move toward the player in 2d. I already made them able to move but I do not know how to make it so they turn around to face me. Heres what I have so far: private GameObject Player; public Transform target; public float speed = 3f;

     void Start() 
     {
         Player = GameObject.Find("Player"); // target
     }
  
      void Update()
      {     
 //move towards player
         if (Vector3.Distance(transform.position, target.position) > 1f) 
         { 
             transform.position = Vector2.MoveTowards(transform.position, Player.transform.position,   speed * Time.deltaTime); 
         }
      }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by sean244 · Jan 20, 2019 at 01:14 AM

alt text

Here’s the script:

 using UnityEngine;
  
 public class Enemy : MonoBehaviour
 {    
 public Transform target;
 public float speed = 3f;
  
 private void Update()
 {
     if (Vector3.Distance(transform.position, target.position) > 1f)
     {
         MoveTowardsTarget();
         RotateTowardsTarget();
     }
 }
  
 private void MoveTowardsTarget()
 {
     transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
 }
  
 private void RotateTowardsTarget()
 {
     var offset = 90f;
     Vector2 direction = target.position - transform.position;
     direction.Normalize();
     float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;       
     transform.rotation = Quaternion.Euler(Vector3.forward * (angle + offset));
 }
 }



enemyrotation.gif (315.5 kB)
Comment
Add comment · Show 7 · 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 danielg8 · Jan 20, 2019 at 01:27 AM 0
Share

Good point. I dunno why I referenced the player twice. But thanks so much for the answer!

avatar image sean244 danielg8 · Jan 20, 2019 at 01:34 AM 1
Share

Your welcome. Keep in $$anonymous$$d that at the very beginning, the enemy will instantly snap to the right rotation. If you don't like this, you can have it more smoothly rotate towards it instead. Just create a float called 'rotationSpeed' and replace

 transform.rotation = Quaternion.Euler(Vector3.forward * (angle + offset));

with these two lines

 Quaternion rotation = Quaternion.AngleAxis(angle + offset, Vector3.forward);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
avatar image KaNgUrUh · Nov 19, 2020 at 12:48 PM 1
Share

I want you to know sir, that you, yes you, are a god among men. I spent HOURS trying to make a fireball rotate in the direction of its path. You saved my night.

avatar image sean244 KaNgUrUh · Nov 19, 2020 at 06:32 PM 0
Share

You're welcome :)

avatar image mukasv sean244 · Feb 03 at 12:25 PM 0
Share

Why did you need to normalize? And this variable angle, what calculation does it return?

Show more comments
avatar image
0

Answer by tormentoarmagedoom · Jan 20, 2019 at 12:46 AM

Good day.

Did you tried transform.LookAt() ?

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 danielg8 · Jan 20, 2019 at 01:25 AM 0
Share

Yeah so I just tried adding transform.LookAt(target) to my script and the weirdest thing happens: all of my enemies disappear and fall off the map. $$anonymous$$ust be a glitch with unity

avatar image Toscan0 danielg8 · Apr 13, 2020 at 12:29 AM 0
Share

Did u set the correct target? I dont belive its a glitch.

Since is a 2d game, i guess that u are using 2d sprites, if u rotate a sprite 90 degres he desapers, because we dosen't pcupe space in the z axis, like a plan

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

208 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 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

Follower Airplane problem 1 Answer

Enemy Rotation AI 0 Answers

How do i get this guy to hold his position? 3 Answers

Unity AI Obstacle Avoidance Help 3 Answers

2D AI: How to get the AI to aim ahead of its target? 3 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