enemy attack AI

trying to add a script to an enemy but the errors that i have on the script are odd to me its saying expecting } found " but there are no " anywhere on that line of code please someone assist me…idk how to put the code in the box on here please bare with me…

var attack : int = 5;
var nextAttack : int = 0;

function Update () {

playerDist = Vector3.Distance(transform.position , player.position);

if(playerDist <= attackDist){

transform.LookAt(player);
animation.CrossFade (“attack”);

Attack();

return;

}

if(playerDist <= seekplayerDist){

transform.LookAt(player);
transform.position += transform.forward * speed * Time.deltaTime;
animation.CrossFade (“run”);

} else {

animation.CrossFade (“idle”);

}

}

function Attack (){

var attackRate : int = 5;
var nextAttack : int = 0;

attackStrenght = weaponDamage + strenght;
damage = (opponentAC) - (attackStrenght);
weaponDamage = Random.Range(1.0, 6.0);

if (Time.time > nextAttack) {

nextAttack = Time.time + attackRate;
//playerStats.health += damage;
}

Use Code tags…

I only found that you missed a } at the end.

Maybe seperate your { } on different lines if you have problems… I use this as habit now a days. Its easier to see what bracket matches the other.

Example:

public class MyClass
{
	public void SomeMethod()
	{
		if(IsSomethingHappening())
		{
			//Do something
		}
	}

	private bool IsSomethingHappening()
	{
		return true;
	}
}
var attack : int = 5;
var nextAttack : int = 0;

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

	if(playerDist <= attackDist) {
		transform.LookAt(player);
		animation.CrossFade ("attack");
		Attack();
		return;
	}

	if(playerDist <= seekplayerDist) {
		transform.LookAt(player);
		transform.position += transform.forward * speed * Time.deltaTime;
		animation.CrossFade ("run");
	} else {
		animation.CrossFade ("idle");
	}
}


function Attack () {
	var attackRate : int = 5;
	var nextAttack : int = 0;

	attackStrength = weaponDamage + attackStrength;
	damage = (opponentAC) - (attackStrength);
	weaponDamage = Random.Range(1.0f, 6.0f);

	if (Time.time > nextAttack) {

		nextAttack = Time.time + attackRate;
		//playerStats.health += damage;
	}
} // <--- You missed this bracket.

Hi ks9109,

You are missing a } at the end of your script.

The last } is closing the if statement, and Attack() is left open.

Regards,
Ezro

thank you for the help as soon as i added a bracket it worked but then there were many more errors that were not seen by my naked eye

Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(8,52): BCE0005: Unknown identifier: ‘player’.
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(10,18): BCE0005: Unknown identifier: ‘attackDist’.
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(14,18): BCE0005: Unknown identifier: ‘player’.
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(24,18): BCE0005: Unknown identifier: ‘seekplayerDist’.
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(26,18): BCE0005: Unknown identifier: ‘player’.
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(27,43): BCE0005: Unknown identifier: ‘speed’.
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(49,18): BCE0005: Unknown identifier: ‘weaponDamage’.
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(49,33): BCE0005: Unknown identifier: ‘strenght’.
Assets/Assets/Asset Folder/Droid Girl u4/Fbx/EnemyAttack.js(50,11): BCE0005: Unknown identifier: ‘opponentAC’.

any help…

ks9109,

It’s hard to provide help without seeing the full code, but those errors are because you don’t have the variables defined within their relative scope.

Regards,
Ezro

when you say that you mean like

var player
var attackDist
etc…
right Ezro

[CODE

]var attack : int = 5;
var nextAttack : int = 0;
var attackDist;
var weaponDamage;
var strength;
var opponentAC;
var Speed;
var seekplayerDist;

function Update () {

playerDist = Vector3.Distance(transform.position , player.position);

if(playerDist <= attackDist){

transform.LookAt(player);
animation.CrossFade (“attack”);

Attack();

return;

}

if(playerDist <= seekplayerDist){

transform.LookAt(player);
transform.position += transform.forward * speed * Time.deltaTime;
animation.CrossFade (“run”);

} else {

animation.CrossFade (“idle”);

}

}

function Attack (){

var attackRate : int = 5;
var nextAttack : int = 0;

attackStrenght = weaponDamage + strenght;
damage = (opponentAC) - (attackStrenght);
weaponDamage = Random.Range(1.0, 6.0);

if (Time.time > nextAttack) {

nextAttack = Time.time + attackRate;
//playerStats.health += damage;
}
[/CODE]