• 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
1
Question by vikingallday1 · Aug 11, 2017 at 11:02 PM · 2d gamerotate object2d spritesrotatetowardslook at

2D Top Down how to get enemy to face my player correctly?

I am making a 2D top down game, and I want my enemy to face my player and follow them. The follow part works, but the rotation is off. The enemy rotates as I move around it, but its not actually looking at me. When the enemy is horizontal to me, it's looking down, not to me. If the enemy is vertical to me, it faces to the right or to the left, not to me.

Here is my code (bottom half is to follow the player):

 transform.eulerAngles = new Vector3 (0, 0, -transform.eulerAngles.z); 
         transform.LookAt(target.position, transform.up);
         transform.Rotate(new Vector3(0,-90,0),Space.Self);//correcting the original rotation 
 
 
 
         //move towards the player
         if (Vector3.Distance (transform.position, target.position) > 0.5f) {//move if distance from target is greater than 1
             
         transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));
Comment
Add comment · Show 5
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 unidad2pete · Aug 11, 2017 at 11:26 PM 0
Share

dont works only with this line?

 transform.LookAt(target.position);
avatar image vikingallday1 unidad2pete · Aug 11, 2017 at 11:34 PM 0
Share

It works with that line in 3D, I need it to only rotate the enemy on the Z Axis, thay rotates it on the y and x axis too, so the enemy goes invisible.

avatar image unidad2pete vikingallday1 · Aug 11, 2017 at 11:45 PM 0
Share

try this

 transform.LookAt(new Vector3(transform.position.x, transform.position.y, target.position.z) 
Show more comments
avatar image lucassivolella · Aug 19, 2020 at 09:24 PM 0
Share

@vikingallday1 damn, it fitted like a glove! Just had to change the -90 to 90 because my sprite was facing the left. To think I almost gave up a went with a simple ball sprite.

You rock dude.

2 Replies

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

Answer by test45 · Aug 13, 2017 at 01:55 AM

Maybe the game object is looking at you!

Maybe the sprite renderer you're using is rotated on the game object incorrectly.

Create an empty and add the sprite prefab as a child, then you can rotate the sprite independently of the game object.

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 vikingallday1 · Aug 15, 2017 at 01:40 AM 0
Share

All I had to do was flip the image around in the sprite editor... thanks!

avatar image test45 vikingallday1 · Aug 15, 2017 at 01:35 PM 0
Share

It's ok I had the same issue.

avatar image
0

Answer by vardevelopment · Aug 12, 2017 at 12:20 AM

Try this: Vector3 direction = target.position - this.transform.position; float angle = Mathf.Atan2(direction.y, direction.x); this.transform.rotation = Quaternion.Euler(0f, 0f, angle * Mathf.Rad2Deg);

Comment
Add comment · Show 4 · 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 vardevelopment · Aug 12, 2017 at 12:23 AM 0
Share

I don't know why that code is all messed up because I wrote it in the insert code tab. Well anyway, Just make a new line after the semicolons. Also, I didn't really read your code too thoroughly but this is how I rotate in 2d

avatar image vikingallday1 · Aug 12, 2017 at 12:45 AM 0
Share

That makes his rotation more smoother which is good, but he still isn't facing me. He still is facing down. I've tried starting him facing at me when I start the game, but the second I start it, he the faces down.

avatar image vardevelopment vikingallday1 · Aug 12, 2017 at 03:04 PM 0
Share

Try changing the euler angles. If he is facing the opposite direction of what you want, do this:

 Quaternion.Euler(0f,0f,angle * $$anonymous$$athf.Rad2Deg - 180f)

 

The -180 flips the direction. So - 90 would turn the object 1/4 a revolution.

avatar image vikingallday1 vardevelopment · Aug 12, 2017 at 10:55 PM 0
Share

using the -90 makes it so he spins in a circle around me, and making it 180 has him just move away from me

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

77 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

Related Questions

how do I fix the quality of my sprites when they are scaled down to fit in the camera? 1 Answer

How do I flip a sprite based on parent's rotation? 1 Answer

4 direction animation unity 2d 3 Answers

Tracking the object in the 2D game 0 Answers

Image swapping sprites delay. 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