- Home /
Too Close To The Trap and melee attacking enemys.
I created an enemy that attacks when i get close to him. When I get close, it should coming out a bit from the water and attack me. The problem it that it don't works untill I get too much close to him, I meen that insted of it will start to attack me from 10 meters range, it attacks me from 1 meter range or less. I tried with 500 meters and it still doesn't works. How do I solve the problem?
Here is my script if something wrong there:
var distance; var target : Transform; var speed = 4.0; var landDistance; var land : Transform; var attackRange = 10.0; var floteRange = 2.0; var goUp = false; static var FIRE = false;
function Update () { distance = Vector3.Distance(target.position, transform.position); landDistance = Vector3.Distance(land.position, transform.position);
if (distance < attackRange)
{
goUp = true;
}
else
{
FIRE = false;
}
if (goUp)
{
if (landDistance <= floteRange)
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
else
{
FIRE = true;
}
}
}
One more thing that i want to know is to create melee attack enemy. I don't know how to start, from where or anything else... Someone can tell me how to begin or the basics? It not something importent, it just can be fun to do...
Sorry for poor english...
Answer by GesterX · May 03, 2011 at 04:00 PM
Your code looks fine.
Make sure that you are changing the value of attackRange in the inspector and not in the script. For some reason I have found that once the script is attached to the object - changing the values in the script do not change the values for the object. You need to change them in the object inspector itself.
It not really the problem... I did changed the theme in the object inspector. But I solved the problem like that: 10 meters is not enough!!! It too close to him, now it 40 meters and it's enough. 10 meters looks like 1 meter in my game...
Okay. You should note that the "distances" in Unity are not in "meters" they are just a representation of the co-ordinate system.
So 500 meters did nothing? But now 40 meters does? floteRange
is a little odd -- it says the monster has to stay within a circle to attack.
$$anonymous$$eters, not meters; 500 meters, 40 meters; the problem solved... Thats it... No more questions here...
Your answer
![](https://koobas.hobune.stream/wayback/20220613131149im_/https://answers.unity.com/themes/thub/images/avi.jpg)