Adding Object Avoidance to my Enemy AI Script

I’m really having trouble adding the ability for my enemy to avoid onjects. I know I should be using some kind of ray-casting but I just can’ figure it out. Any help is very much appreciated. Here is my code without the object avoiding:

var player : Transform;
var speed : float=5;
var range : float=15;
var hitRange : float=6;
var enemyDamage : float=10;
var rotationSpeed : float=5;
var damageTimer : float=0;
var delta : Vector3;
var distance;

function Update()
distance = Vector3.Distance(transform.position, player.transform.position);

var moveSpeed =; // this variable stores the current velocity
var gravity: float = 20; // gravity like in the Move example

function MoveTowards()
if(distance<=(hitRange/1.5)) return;
var character = GetComponent(CharacterController);
if (character.isGrounded){
    delta = player.transform.position - transform.position;
    delta.y = 0; // zero delta.y before normalization!
    transform.forward = delta; // turn enemy to the player
    moveSpeed = delta * speed;
moveSpeed.y -= gravity * Time.deltaTime; // apply gravity acceleration
character.Move(moveSpeed * Time.deltaTime); // move the character

function AttackPlayer()
if (distance < hitRange && damageTimer>=1.5)
    ("ApplyDamage", enemyDamage, SendMessageOptions.DontRequireReceiver);

To use ray casting for object avoidance, what you do is draw, say, 5 rays out of the front of the enemy covering the front 180°, so that if, for example, a ray on the left hand side hits a collider, the enemy with rotate a bit to the right every frame until that ray is no longer hitting a collider
Hope this helps a bit :slight_smile:
A good place to learn how to use ray casting is the unity page below: