Ai help

Hi,

I am working on an AI enemy. I made it work but i want to deal damage. I made a script that is:

var ApplyAIAttack : float = 1.5;
var AIDamage = 25;
var AIDistanceFromPlayer : float;

function Update ();
{
if (Distance < ApplyAIAttack)
{
SendMessage(“AIAttackDealt”, SendMessageOptions.DontRequireReceiver);
}
}

And then i made another script for the player to have HP and lose some and die after. It is.

#pragma strict

var PlayerHealth = 100;

function Update ()
{
if(PlayerHealth <= 0 )
{
Killed();
}
}

function ApplyDammage (AIDamage : int)
{
PlayerHealth -= AIDamage;
}

function Killed()
{
Destroy (gameObject);
}

This script did not work so can someone fix it please.

Thanks,

-Skace Studio

How did you get “distance” in the first script? As far as I can tell, that just popped up out of nowhere; you need to find the distance first. Start here:

Can you please write it out how it should be. I am new in unity so i need help. Thanks

There actually seem to be quite a few flaws with your script and it is unclear what you want to achieve. If you were to specify it, we might be able to help. These are some aspects you have to consider

  1. do you want the ai to deal damage constantly? That means if for example he deals 25 damage per hit then the amount of damage he deals in 1 second is 25*60(frames per second) = 1500.
  2. or the more professional looking way is to add some kind of cooldown time, so that after the enemy attacks it would have to wait for example a second before attacking again. So the damage dealt in a second would be 25.

No, but I can help you out :slight_smile:

First, you need to get the distance between the enemy and the player. To do that, you need to get the player’s transform. So, first, you need to get it by doing this in the awake function:

var playerTransform: Transform;

function Awake()
{
    playerTransform = GameObject.FindWithTag("Player").transform;
    print(playerTransform.position);
}

That code should print the player's transform in the console.  Once you get that working, repost your code and we'll go from there. :)

(-399.6, 73.3, 266.9)
UnityEngine.MonoBehaviour:print(Object)
Script:Awake() (at Assets/Scripts/Script.js:11)

OK, awesome :slight_smile:

Now, we can move onto your if statement.

Right now, your code says this:

var ApplyAIAttack : float = 1.5;
var AIDamage = 25;
var AIDistanceFromPlayer : float;

function Update ();
{
if (Distance < ApplyAIAttack)
{
SendMessage("AIAttackDealt", SendMessageOptions.DontRequireReceiver);
}
}

However, in your if statement, you just say distance. Unity has no idea what distance is, or even that it’s a number. So, we need to tell it that. In order to find the distance between the AI and the player, we need the positions of both objects. Then, we plug them into the Vector3.Distance function. So, your if statement should look something like this:

if (Vector3.Distance(POSITION1, transform.position) < ApplyAIAttack)

Now, I left out the player’s position (POSITION1). Given that we already have the transform of the player saved in the variable playerTransform, we now just need to access the position. See if you can figure it out from here. :slight_smile:

As far as I can tell, it SHOULD work. Let me know if it does, and if it doesn’t post your code.

Thanks for the help but there where errors in the console i tried to fix the errors using the consoleand it ended up like this :
var playerTransform: Transform;
var ApplyAIAttack : float = 1.5;
var AIDamage = 25;
var AIDistanceFromPlayer : float;

function Update ()
{

if (Distance < ApplyAIAttack)

{
SendMessage(“AIAttackDealt”, SendMessageOptions.DontRequireReceiver);
}

function Awake();

{

playerTransform : GameObject.FindWithTag(“Player”).transform};

print(playerTransform.position);

}

And then there was one last error i didnt understand it said:
Assets/Scripts/Script.js(15,10): BCE0044: expecting (, found ‘Awake’.

The last error was because you forgot a curly brace at the end of the update function. Also, there are no semicolons after function names.

This is the code:

    var ApplyAIAttack : float = 1.5;
    var AIDamage : float = 25;
    var AIDistanceFromPlayer : float;
   //The variable in which we will store the player transform
    var playerTransform : Transform;
     
    function Update ()
    {
          //Get the distance between the enemy and the player and check if it is close enough to deal damage
          if (Vector3.Distance(playerTransform.position , transform.position) < ApplyAIAttack)
         {
               SendMessage("AIAttackDealt", SendMessageOptions.DontRequireReceiver);
         }

//You forgot this curly bracket
    }



function Awake()
{
       //Save the player transform in the variable playerTransform
       playerTransform = GameObject.FindWithTag("Player").transform};
}

Now the enemy should damage the player when in range. I think. let me know if it works :slight_smile:

EDIT: I made a few changes to the code, to fix some errors. Sorry.

It says it has 2 errors they are
Assets/Scripts/Test.js(23,72): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Scripts/Test.js(24,1): BCE0044: expecting EOF, found ‘}’.

    var ApplyAIAttack : float = 1.5;
    var AIDamage : float = 25;
    var AIDistanceFromPlayer : float;
   //The variable in which we will store the player transform
    var playerTransform : Transform;
     
    function Update ()
    {
          //Get the distance between the enemy and the player and check if it is close enough to deal damage
          if (Vector3.Distance(playerTransform.position , transform.position) < ApplyAIAttack)
         {
               SendMessage("AIAttackDealt", SendMessageOptions.DontRequireReceiver);
         }

//You forgot this curly bracket
    }



function Awake()
{
       //Save the player transform in the variable playerTransform
       playerTransform = GameObject.FindWithTag("Player").transform;//<--There was a random closing bracket here. Removing it should fix it
}

Check the code above, there was a random bracket there that I removed. (Everyone makes mistakes)
You should be good to go now. The above code should work.

Not to discourage you but I think you ought to take some coding lessons and return to the project afterwards. Or maybe follow some youtube tutorial for beginners to get the hang of it. Currently you don’t seem to be understanding much of what is going on in the code and you can’t just keep asking for help on the forums. And don’t use any code in your project that you don’t understand. That’ll only create more confusion afterwards. Don’t be afraid to ask what a certain code snippet is doing.

Other than that,
Best of luck with your project.

Thanks for noticing that :slight_smile:

Thanks I tryed the script and it says there are no errors but it does not do any damage. I am puting the project on to drop box so you can check it.
i will put the link on later

It is probably because you don’t have a function named AIAttackDealt. What you probably want is to replace the send message line

SendMessage("AIAttackDealt", SendMessageOptions.DontRequireReceiver);

with this

 playerTransform.gameObject.SendMessage("ApplyDammage",AIDamage, SendMessageOptions.DontRequireReceiver);

Your function name has 2 ‘M’ letters in it so I set the send message according to that, I suggest you fix both.
I haven’t tested the code, but it should work.

i have made a script to recieve it. it was:
#pragma strict

var PlayerHealth = 100;

function Update ()
{
if(PlayerHealth <= 0 )
{
Killed();
}
}

function ApplyDammage (AIDamage : int)
{
PlayerHealth -= AIDamage;
}

function Killed()
{
Destroy (gameObject);
}

The script might be there but the reference from the other script doesn’t match the one that should receive it.

Did you try replacing the line of code as I suggested in the previous post?
Try that out and report back the changes.

it still does not work

Is the script with player health added as a component to the Player object.
If it is, try removing the SendMessage.DontRequireReceiver from that line and see if that gives you a error on runtime.
If so post that error message.

The script with the health is attached to the player.

But did you try removing the SendMessage.DontRequireReceiver
like that

playerTransform.gameObject.SendMessage("ApplyDammage",AIDamage);