[HELP] How to create an AI script that will avoid obstacles by moving up or down in 2D Environment.?

Hi everyone this is my first post. So pls guide me if im doing something wrong.

What i need to achieve is, i have a 2d game object (for example a flying rocket) this rocket must avoid the incoming obstacles automaticaly by moving up or down(for example incoming mountains) which are also 2d objects,i searched a lot and came up with the following code,but its for 3d,so pls can anyone guide me in this…

Please guys help me with this… :frowning:

using UnityEngine;
using System.Collections;

public class EnemyAI: MonoBehaviour {
// Fix a range how early u want your enemy detect the obstacle.
private int range;
private float speed;
private bool isThereAnyThing = false;

// Specify the target for the enemy.
public GameObject target;
private float rotationSpeed;
private RaycastHit hit;
// Use this for initialization
void Start() {
  range = 80;
  speed = 10f;
  rotationSpeed = 15f;
}

// Update is called once per frame
void Update() {
  //Look At Somthly Towards the Target if there is nothing in front.
  if (!isThereAnyThing) {
   Vector3 relativePos = target.transform.position - transform.position;
   Quaternion rotation = Quaternion.LookRotation(relativePos);
   transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
  }

  // Enemy translate in forward direction.
  transform.Translate(Vector3.forward * Time.deltaTime * speed);

  //Checking for any Obstacle in front.
  // Two rays left and right to the object to detect the obstacle.
  Transform leftRay = transform;
  Transform rightRay = transform;

  //Use Phyics.RayCast to detect the obstacle

  if (Physics.Raycast(leftRay.position + (transform.right * 7), transform.forward, out hit, range) || Physics.Raycast(rightRay.position - (transform.right * 7), transform.forward, out hit, range)) {

   if (hit.collider.gameObject.CompareTag("Obstacles")) {
    isThereAnyThing = true;
    transform.Rotate(Vector3.up * Time.deltaTime * rotationSpeed);
   }
  }

  // Now Two More RayCast At The End of Object to detect that object has already pass the obsatacle.
  // Just making this boolean variable false it means there is nothing in front of object.
  if (Physics.Raycast(transform.position - (transform.forward * 4), transform.right, out hit, 10) ||
   Physics.Raycast(transform.position - (transform.forward * 4), -transform.right, out hit, 10)) {
   if (hit.collider.gameObject.CompareTag("Obstacles")) {

    isThereAnyThing = false;
   }
  }

  // Use to debug the Physics.RayCast.
  Debug.DrawRay(transform.position + (transform.right * 7), transform.forward * 20, Color.red);

  Debug.DrawRay(transform.position - (transform.right * 7), transform.forward * 20, Color.red);

  Debug.DrawRay(transform.position - (transform.forward * 4), -transform.right * 20, Color.yellow);

  Debug.DrawRay(transform.position - (transform.forward * 4), transform.right * 20, Color.yellow);

}
}

3D code works in 2D with no restrictions (as far as I know).

You’d probably want to raycast in front of the object to tell if there is another GameObject in its path.

Then you could use transform.Translate or Rigidbody.AddForce or Rigidbody.MovePosition, depending on what exact effect you’re trying to achieve.

Copy-pasting code you find with little understanding on how it works won’t get you very far; and it will be extremely frustrating to try to use something when you don’t know how it works. So I recommend doing some tutorials (such as Roll-A-Ball) to get a grasp of coding and Unity in general.

1 Like