I’m making a 2D game in which your a small girl fighting of ghosts. I’ve written code that moves the ghosts toward the player using Vector3.Lerp which is working as I intended. I want to use Vector3.Distance to find how far away the enemies are away from my character, so far I’ve been able to get a value for the distance and that seems to be working. But I’m having problems with how to remove the players health or “Sanity” at a faster rate depending on how close the enemies are.
This is what I’ve come up with so far:
#pragma strict
var CharacterPos : Vector3;
var EnemyPos : Vector3;
static var Distance1 : Vector3;
var SanityValue: int;
function Update ()
{
var Distance1 : Transform;
var dist = Vector3.Distance((CharacterMover.playerPos), (EnemyMovement2.EnemyPos));
print ("Distance to other: " + dist);
SanityRemover();
print (SanityValue);
}
function SanityRemover() {
if ((Distance1 > 4) & (Distance1 < 5)) {
SanityValue -= 1 * Time.deltaTime;
}
else if ((Distance1 > 3) & (Distance1 < 4)) {
SanityValue -= 2 * Time.deltaTime;
}
else if ((Distance1 > 2) & (Distance1 < 3)){
SanityValue -= 3 * Time.deltaTime;
}
else if ((Distance1 > 1) & (Distance1 < 2)) {
SanityValue -= 4 * Time.deltaTime;
}
else if ((Distance1 > 0) & (Distance1 < 1)) {
SanityValue -= 5 * Time.deltaTime;
}
}
I’m getting the following errors:
Operator ‘<’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘int’.
Operator ‘>’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘int’.
Also, is the way I’ve coded the if statements the best way of doing it?
Thanks in advance!