• 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 APERSONWHOISAPERSON · Feb 22, 2017 at 02:13 AM · unity 5movementleftright

How to determine if an Enemy is moving left or right?

First, here's the code

 using UnityEngine;
 using System.Collections;
 
 public class BMoveAI : MonoBehaviour
 {
     public Vector2 pos1;
     public Vector2 pos2;
     public float speed;
 
     void Update()
     {
         transform.position = Vector3.Lerp (pos1, pos2, Mathf.PingPong (Time.time * speed, 1.0f));
     }
 }

So what I'm trying to do is make my Enemy's sprite flip depending on whether he is moving left or right. So my question is how I would detect moving left or right?

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

Answer by Zee-Play · Feb 22, 2017 at 04:07 AM

You could store your character's Y position in a variable, and then in the next frame check whether the new position is smaller or greater than the last one. (Assuming Y axis is your horizontal, and it's that way ->) You might need to swap left and right depending on what your sprite looks like.

 using UnityEngine;
 using System.Collections;
  
 private float oldPosition = 0.0f
 
 public class BMoveAI : MonoBehaviour
 {
     public Vector2 pos1;
     public Vector2 pos2;
     public float speed;
 
     void Start()
     {
         oldPosition = transform.position.y;
     }
 
      void Update()
     {
         transform.position = Vector3.Lerp (pos1, pos2, Mathf.PingPong (Time.time * speed, 1.0f));
 
         if (transform.position.y > oldPosition) // he's looking right
         {
             transform.localRotation = Quaternion.Euler(0, 0, 0);
         }
 
         if (transform.position.y < oldPosition) // he's looking left
         {
             transform.localRotation = Quaternion.Euler(0, 180, 0);
         }
         oldPosition = transform.position.y; // update the variable with the new position so we can chack against it next frame
     }
 }

Comment
Add comment · Show 3 · 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 APERSONWHOISAPERSON · Feb 22, 2017 at 05:45 AM 0
Share

Thank you for the answer! However now when I start the game: The sprite just flips and then doesn't change... Any help is appreciated. :)

avatar image Zee-Play APERSONWHOISAPERSON · Feb 22, 2017 at 11:53 AM 0
Share

Try this, tested and it works

 using UnityEngine;
 using System.Collections;
 
  public class B$$anonymous$$oveAI : $$anonymous$$onoBehaviour
 {
     public Vector2 pos1;
     public Vector2 pos2;
     public float speed;
     private float oldPosition = 0.0f;
 
     
     void Start()
     {
         oldPosition = transform.position.x;
     }
 
     void Update()
     {
         transform.position = Vector3.Lerp(pos1, pos2, $$anonymous$$athf.PingPong(Time.time * speed, 1.0f));
 
         if (transform.position.x > oldPosition) // he's looking right
         {
             transform.localRotation = Quaternion.Euler(0, 0, 0);
         }
 
         if (transform.position.x < oldPosition) // he's looking left
         {
             transform.localRotation = Quaternion.Euler(0, 180, 0);
         }
         oldPosition = transform.position.x; // update the variable with the new position so we can chack against it next frame
     }
 }
avatar image APERSONWHOISAPERSON Zee-Play · Feb 22, 2017 at 07:04 PM 0
Share

Yes! It works! Thank you for your help Zee-Play. :)

avatar image
0

Answer by asim15566 · Sep 05, 2021 at 09:28 AM

You can also try this one

        public float oldPosition;
        public bool movingRight = false;
        public bool movingLeft = false;


      void LateUpdate(){
     oldPosition = transform.position.x;
 }

 void Update () {
     
     if (transform.position.x > oldPosition) {
         movingRight = true;
         movingLeft = false;
     }
     if (transform.position.x < oldPosition) {
         movingRight = false;
         movingLeft = true;
     }
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

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

156 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

Related Questions

problem with scripting movement 1 Answer

so how to i create a moving barrel 0 Answers

First person controller going backwards 0 Answers

My objects can only move if rotation is on, with what seems like a friction issue. 0 Answers

Attaching a gameObject above a rolling ball 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