• 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 Yenway · Aug 11, 2014 at 01:09 AM · transforms

Positioning isn't being detected correctly.

Quick background: It's a 2D game, and I'm trying to get an enemy to detect if the player is on their left or the right, and change facing accordingly. However, enemies aren't picking up on the facing consistently. Here's the relevant code:

     Animator animator;
     GameObject homeTarget;
     Vector2 targetPosition;
 
     void Start()
     {
         homeTarget = GameObject.Find ("Player");
         animator = this.GetComponent<Animator> ();
 
     }
     
     void Update () 
     {
         if (GameObject.Find ("Player") != null) 
         {
             Vector2 targetPosition = homeTarget.transform.position;
 
             if (targetPosition.x < transform.position.x)
             {
                 animator.SetInteger("Direction", 0);
                 Debug.Log ("Now facing left");
             }
             else if (targetPosition.y > transform.position.x)
             {
                 animator.SetInteger("Direction", 1);
                 Debug.Log ("Now facing right");
             }
             else
             {
             }
                 
         }
 
     
     }

The "Now facing left/right" message doesn't fire the instant you swap sides. It's especially bad when you move over to the enemy's right side, but other than that, I haven't been able to find a pattern.

Comment
Add comment · Show 1
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 Yenway · Aug 11, 2014 at 03:40 AM 0
Share

D'oh. I knew it was going to be something terribly obvious. Thanks!

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dhandley · Aug 11, 2014 at 03:39 AM

You're testing against the y axis to detect if the player is on the right. You should test the x axis instead:

 else if (targetPosition.x > transform.position.x)
 {
     animator.SetInteger("Direction", 1);
     Debug.Log ("Now facing right");
 }







Comment
Add comment · 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
0

Answer by Fadolf · Aug 11, 2014 at 03:39 AM

OK, you need to know the angle between the two objects, this will allow you to know where the player is. Then, you can do whatever you want, in this example, set the correct animation I guess.

I added a Debug.Drawline to see if it was working correctly and also a Debug.Log.

Note the line with if(player) or if(player !=null) with the comment above.

 public class test2D : MonoBehaviour {
 
     GameObject player;
     Vector2 enemyPos;
     Vector2 playerPos;
 
     void Start () {
         player = GameObject.Find("Player");
     }
     void Update () {
 
         //Note that there is no need to find the Gameobject again.
         //This will increase performance as finding objects has a big impact.
         if (player)
         {
             playerPos = player.transform.position;
             enemyPos = transform.position;
 
             //Angle in radians between the two Gameobjects.
             float rawAngle = Mathf.Atan2(enemyPos.y - playerPos.y, enemyPos.x - playerPos.x);
 
             
             //Mathf.Abs because we only want to know either it is left or right, not up/down.
             float angle = Mathf.Abs(rawAngle * Mathf.Rad2Deg);
             //Conversion to degrees............^^^^^^^^^^^^^^
 
             Debug.Log(angle);
 
             if (angle < 90f) Debug.Log("FACING LEFT");
             else if (angle > 90f) Debug.Log("FACING RIGHT");
 
 
             
             Debug.DrawLine(transform.position, player.transform.position,Color.red);
 
 
         }
         
     
     }
 }
 




Sorry for bad drawing skills. alt text

Comment
Add comment · 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
0

Answer by robertbu · Aug 11, 2014 at 03:38 AM

In line 23:

  if (targetPosition.y > transform.position.x)

...you are comparing 'y' to 'x', when you want to compare 'x' to 'x'. In addition, unless you have a separate visual for neither left or right, then the 'if' is not necessary at all.

  void Update () 
  {
     if (GameObject.Find ("Player") != null) 
     {
         Vector2 targetPosition = homeTarget.transform.position;
 
         if (targetPosition.x < transform.position.x)
         {
             animator.SetInteger("Direction", 0);
             Debug.Log ("Now facing left");
         }
         else
         {
             animator.SetInteger("Direction", 1);
             Debug.Log ("Now facing right");
         }
     }
 }
Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Animating UV transforms in Shader 4 Answers

The script isn't taking in the right transform positions! 1 Answer

Problem combining GyroDroid rotation with independent position changes 0 Answers

Relative transforms and parenting 1 Answer

Smooth Position Change (Ironsights) 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