# can some one help with my AI script?

I’m new to unity and java scripting…
I’ve tried countless times to add gravity to may AI Player. no matter what i do it is ignored bye the system. could someone tell me what to do and where to put it.

Here is my script… thanks

```var player : GameObject;
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 Start()
{
player = GameObject.FindGameObject.transform;
}

function Update()
{
distance = Vector3.Distance(transform.position, player.transform.position);
if(distance<=range)
{
MoveTowards();
RotateTowards();
AttackPlayer();
}
}

function MoveTowards()
{
delta = player.transform.position - transform.position;
delta.Normalize();
delta.y = 0;
if(distance<=(hitRange/1.5))
{
return;
}
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}

function RotateTowards()
{
transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation(delta), rotationSpeed);
transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, 0);
}

function AttackPlayer()
{
damageTimer+=Time.deltaTime;
if (distance < hitRange && damageTimer>=1.5)
{
damageTimer=0f;
player.SendMessageUpwards
}
}
```

EDITED: If you’re using a CharacterController in your enemy, forget about the rigidbody and modify MoveTowards to this:

```var moveSpeed = Vector3.zero; // 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!
delta.Normalize();
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
}
```

CharacterControllers must be moved with Move, and the gravity can be applied like in the Move example.

EDITED 2: The RotateTowards function is giving some runtime errors; you can add the line transform.forward = delta; to MoveTowards like above and eliminate RotateTowards (it’s not rotating gradually anyway - you would need a more complicated routine to do that).

I tried it out with my my own gravity script but it ignored it as well… that is until it got below my platform. If I put the platform that was below the AI above the AI it would act like it had gravity but once the platform was under it again it would just continue to float there… here is my gravity script
var speed : float = 6.0;

var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update()

{

``````var controller : CharacterController = GetComponent(CharacterController);

moveDirection.y -= gravity * Time.deltaTime;

controller.Move(moveDirection * Time.deltaTime);
``````

}

OK i figured it out… for some reason my character had something connected to it which made it act very strange so when i deleted it and put it on another object it worked perfectly. Thank you so much!
O…
and for those who were looking at this for an AI script here it is in one piece:

```var player : GameObject;
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 Start()
{
player = GameObject.GetGameObject.transform;
}

function Update()
{
distance = Vector3.Distance(transform.position, player.transform.position);
if(distance<=range)
{
MoveTowards();
AttackPlayer();
}
}

var moveSpeed = Vector3.zero; // 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!
delta.Normalize();
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()
{
damageTimer+=Time.deltaTime;
if (distance < hitRange && damageTimer>=1.5)
{
damageTimer=0f;
player.SendMessageUpwards
}
}
```

Sorry about that I put my backup script in their here is the real thing…

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);

if(distance<=range)

{

MoveTowards();

AttackPlayer();

}
``````

}

var moveSpeed = Vector3.zero; // 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!

delta.Normalize();

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()

{

``````damageTimer+=Time.deltaTime;

if (distance < hitRange && damageTimer>=1.5)

{

damageTimer=0f;

player.SendMessageUpwards

}
``````

}