How to make my enemy jump over obstacles?

finally i made my first game in unity MY FIRST GAME IN UNITY - YouTube

but now what i want to now is how can i make that when my enemy which is the red one instead pass through the cubes he jump

It’s way more complicated: you must create an AI (Artificial Intelligence) script to control your enemy. It could use Rigidbody.SweepTest with a short distance (1, for instance) to see if the enemy will collide with something ahead, and make it jump if there’s an obstacle. An alternative could be to use a trigger childed to your your enemy and positioned a little ahead, so when something hit it you could make the enemy jump.

EDITED: Well, this is basically your original script with some optimization and the code to jump automatically included - it will jump when colliding to some lateral object (it collides with the ground all the time, so collisions with normals above 30 degrees (sin = 0.5) are ignored. I added also a “jetpack” feature to pass over the abyss - when there is no ground down to 20m below it, the jetpack is turned on until it finds ground. Your enemy must be a CharacterController: if it’s not, add it using the menu option Component/Physics/Character Controller.

using UnityEngine; using System.Collections;

public class Enemy : MonoBehaviour {

	public float speed = 4f;
	public float jetPackSpeed = 0.3f;
	public float jumpSpeed = 8f;
	public float gravity = 10;

	private Transform _Player; 
	private CharacterController character;
	private Transform tr;
	private float vSpeed = 0f;
	private bool jump = false;
	
	void Start ()
	{
		_Player = GameObject.FindGameObjectWithTag("Player").transform;
		character = GetComponent();
		tr = transform;
	}

	void Update ()
	{	// find the vector enemy -> player
		Vector3 chaseDir = _Player.position - tr.position;
		chaseDir.y = 0; // let only the horizontal direction
		float distance = chaseDir.magnitude;  // get the distance
		if (distance <= 2)
			Debug.Log("Attacking Player");
		else
		{	// find the player direction
			Quaternion rot = Quaternion.LookRotation(chaseDir);
			// rotate to his direction
			tr.rotation = Quaternion.Slerp(tr.rotation, rot, Time.deltaTime * 4);
			if (character.isGrounded){ // if is grounded...
				vSpeed = 0;  // vertical speed  is zero
				if (jump){    // if should jump...
					vSpeed = jumpSpeed; // aplly jump speed
					jump = false; // only jump once!
				}
			} 
			else // but if lost ground, check if it's an abyss
			if (!Physics.Raycast(tr.position, -tr.up, 20f)){ // if no ground below
				vSpeed = jetPackSpeed;  // use jetpack
			}
			vSpeed -= gravity * Time.deltaTime; // apply gravity
			// calculate horizontal velocity vector
			chaseDir = chaseDir.normalized * speed;
			chaseDir.y += vSpeed; // include vertical speed
			// and move the enemy
			character.Move(chaseDir * Time.deltaTime);
		}
	}
	
	// if collided with some wall or block, jump
	void OnControllerColliderHit(ControllerColliderHit hit){
		// only check lateral collisions
		if (Mathf.Abs(hit.normal.y) < 0.5){
			jump = true; // jump if collided laterally
		}
	}
}

you could try something like this:

var range = 2;

if(Vector3.Distance(transform.position, target.position) > range