# Raycast AI - Avoiding Obstacles in 2D Topview (Using Velocity)

I need help with a script I’ve been working on. I got the 2D enemy to follow my character, but I’m stuck at trying to make him avoid things with Raycasts. I don’t want him to rotate, because I’m doing a top-down view in 2D. I just want him to change his velocity on the X or Y axis when he approaches a wall. Now, I did write a partial script of him doing that, but I’m too embarrassed to post it because it kinda sucks. (If you want to see it, I can post it in a comment if necessary.) So I will just post the script that makes him follow the player without avoiding things.

Any help would be appreciated.

Thanks

My Script in UnityScript:

``````#pragma strict
//Stuck boolean used for raycast avoiding
//obstacles, I left it here so you can see
//below how I was using it.
var stuck : boolean;
//Storing player's transform into this variable.
var player : Transform;

//Distance between player and enemy.
//May be useful for figuring out if
//he should walk up or down if stuck
//at X axis or Y axis.
var xDistance : float;
var yDistance : float;

var moveSpeed : float;

var anim : Animator;
//Separating X and Y velocities to tell which
//direction the enemy should go.
var xVelocity : float;
var yVelocity : float;

//Boolean for telling the animator when to play
//idle animation.
var isIdle : boolean;

//(haven't coded that yet, but that's not what I need help with)

function Start () {
anim = GetComponent(Animator);
player = GameObject.FindGameObjectWithTag("Player").transform;
}

function FixedUpdate () {

//If both X and Y velocities are at 0
//set Idle animation to true.
if(xVelocity == 0 && yVelocity == 0)
isIdle = true;
else
isIdle = false;

anim.SetBool("isIdle", isIdle);

//Calculating distance from enemy and player.
xDistance = transform.position.x - player.position.x;
yDistance = transform.position.y - player.position.y;

if(follow){

//Setting boolean operations to the position of the enemy.
//Makes it easier to make if statements for where the enemy
var right : boolean;
var left : boolean;
var up : boolean;
var down : boolean;

if(transform.position.x > player.position.x)
right = true;
else
right = false;

if(transform.position.x < player.position.x)
left = true;
else
left = false;

if(transform.position.y > player.position.y)
up = true;
else
up = false;

if(transform.position.y < player.position.y)
down = true;
else
down = false;

//Following the player.
if(right && !stuck)
xVelocity = -moveSpeed;

if(left && !stuck)
xVelocity = moveSpeed;

//It bugs out when the player
//and enemy is on the same X and Y position.
if(Mathf.Round(transform.position.x) ==
Mathf.Round(player.transform.position.x))
xVelocity = 0f;

if(down && !stuck)
yVelocity = moveSpeed;

if(up && !stuck)
yVelocity = -moveSpeed;

//Fixing the wierd thing again for Y position.
if(Mathf.Round(transform.position.y) ==
Mathf.Round(player.position.y) && !stuck)
yVelocity = 0f;
}
//Setting the velocities for the animator.
anim.SetFloat("xVelocity", xVelocity);
anim.SetFloat("yVelocity", yVelocity);
//Using the X and Y velocity variables above to set
//rigidbody velocity.
rigidbody2D.velocity = Vector2(xVelocity, yVelocity);
}
``````

Could you show the avoid collision one?