Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Tageos · Jul 14, 2015 at 07:45 AM · c#2dlookattopdown

Lookat player

Hi there

I am making a 2D top down game. I am now trying to make the enemies always rotate to face the player. Ive just tried this:

 using UnityEngine;
 using System.Collections;
 
 public class ZLookatPlayer : MonoBehaviour {
     
     public Transform Player;
 
     void Update () {
 
             // Rotate the camera every frame so it keeps looking at the Player
             
             transform.LookAt(Player);
 
     }
 }
 

With no success. It seems like the zombies rotates on all 3 axis, making them invisible when vivewing from a 2d perspective. I guess the lookat is only for 3d... Anyway, could anybody help me with a usefull method to make this work in a 2d top-down environment? I have viewed atleast 4 similar threads with no successs.

Thank you in advance!

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
0
Best Answer

Answer by Hellium · Jul 14, 2015 at 08:06 AM

Try with this :

 Vector3 direction = Player.position - transform.position ;

 // Prevent weird behavior if the player and enemy are very close to each other
 if( direction.magnitude > 0.1f )
 {
     // If the target is at the right side of the enemy
     if( Vector3.Dot( Vector3.right, direction ) > 0 )
         transform.rotation = Quaternion.LookRotation( direction ) * Quaternion.Euler( 0, -90, 0 ) ;
     // If the target is at the left side of the enemy, prevent the sprite to flip
     else
         transform.rotation = Quaternion.LookRotation( direction ) * Quaternion.Euler( 0, 90, 180 ) ;

 }
Comment
Add comment · Show 11 · 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 Tageos · Jul 14, 2015 at 09:31 AM 0
Share

This makes them rotate but in a very wierd way. They rotate very slow and randomly "flips" but thanks anyway, its a step on the way :D

avatar image EmreBgdy · Jul 14, 2015 at 09:34 AM 0
Share

Can you use Quaternions at 2D space? Its physically impossible.

avatar image Hellium · Jul 14, 2015 at 09:40 AM 0
Share

@Tageos : I am sorry, I am not familiar with 2D games at all. If I have time, I will try to take a deeper look.

@EmreBgdy : In Unity, you are not restricted to a 2D space, even if you make 2D games. Your sprites just belongs to the (xy) plane to make them fully visible and not distorded, but you still work in a 3D space in fact. Thus, you can use the Quaternions.

avatar image Tageos · Jul 14, 2015 at 10:07 AM 0
Share

@Hellium thank you for taking your time, it would be so much better if they just had a 2D equivalent of the lookat function...

avatar image Hellium · Jul 14, 2015 at 11:02 AM 0
Share

I have found the solution, take a look :

 using UnityEngine;
 using System.Collections;
 
 public class LookAt : $$anonymous$$onoBehaviour {
 
     public Transform target ;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 direction = target.position - transform.position ;
 
         if( direction.magnitude > 0.1f )
         {
             if( Vector3.Dot( Vector3.right, direction ) > 0 )
                 transform.rotation = Quaternion.LookRotation( direction ) * Quaternion.Euler( 0, -90, 0 ) ;
             else
                 transform.rotation = Quaternion.LookRotation( direction ) * Quaternion.Euler( 0, 90, 180 ) ;
         }
     }
 }

I edited the answer so that you can copy-paste it.

Show more comments
avatar image
0

Answer by barbe63 · Jul 14, 2015 at 10:20 AM

Not familiar with 2D too but try this:

 transform.LookAt(new Vector3(Player.position.x, transform.position.y, Player.position.z));
 

Not entirely sure about the axis being right...

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 Tageos · Jul 14, 2015 at 12:43 PM 0
Share

this made them rotate around the y/x axis making them appear invisible.

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

23 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

Related Questions

topdown space game 1 Answer

Distribute terrain in zones 3 Answers

How to make a moving enemy's eye to follow player? 1 Answer

How to make LineRenderer in GameObject Facing? 1 Answer

How do I make my enemy have animations for directions during movemvent 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