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