how to make enemy go in one direction unity 2d

 using System.Collections; using System.Collections.Generic; using UnityEngine;  public class EnemyPatrol : MonoBehaviour {     public float speed;     public float distance;      private bool movingRight = true;      public Transform groundDetection;      void Update()     {         transform.Translate(Vector2.right * speed * Time.deltaTime);          RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);         if (groundInfo.collider == false)         {             if (movingRight == true)             {                 transform.eulerAngles = new Vector3(0, -180, 0);                 movingRight = false;             }             else             {                 transform.eulerAngles = new Vector3(0, 0, 0);                 movingRight = true;             }                    }         private void OnCollisionEnter2D(Collision2D collision)     if (collide.collider.gameObject.tag == "Wall")         {             Debug.Log("E");             if (movingRight == true)             {                 transform.eulerAngles = new Vector3(0, -180, 0);                 movingRight = false;             }             else             {                 transform.eulerAngles = new Vector3(0, 0, 0);                 movingRight = true;             }          }     } }

Are there any code examples left?
New code examples in category Go
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source