- Home /
Decrease players health depending on distance of enemy.
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!
First of all, you should use && ins$$anonymous$$d of just & for this.
Second, you're comparing a Transform to an integer-- you're comparing Distance1 ins$$anonymous$$d of the dist variable.
Okay, so I've changed what you said was incorrect, thanks for the help. :) Here's the updated code. #pragma strict var CharacterPos : Vector3; var EnemyPos : Vector3; static var Distance1 : Vector3; var SanityValue: int; private var dist : int;
function LateUpdate ()
{
var Distance1 : Transform;
var dist = Vector3.Distance((Character$$anonymous$$over.playerPos), (Enemy$$anonymous$$ovement2.EnemyPos));
//print ("Distance to other: " + dist);
print (SanityValue);
SanityRemover();
}
function SanityRemover() {
if ((dist > 4) && (dist < 5)) {
SanityValue -= 1 * Time.deltaTime;
}
else if ((dist > 3) && (dist < 4)) {
SanityValue -= 2 * Time.deltaTime;
}
else if ((dist > 2) && (dist < 3)){
SanityValue -= 3 * Time.deltaTime;
}
else if ((dist > 1) && (dist < 2)) {
SanityValue -= 4 * Time.deltaTime;
}
else if ((dist > 0) && (dist < 1)) {
SanityValue -= 5 * Time.deltaTime;
}
}
But now I'm having other problems, the sanity value doesn't seem to be working at all. It just fluctuate between 0 and 100 constantly any idea's what may be causing this?
Thanks again.
Thank you so much! It works perfectly. I really appreciate it, I wouldn't have got close to making it work without you. :D
No problem, I hope you were able to learn something from it^^
Answer by agamedesigner · Mar 24, 2013 at 08:04 PM
Try this:
#pragma strict
var SanityValue: float = 100;
private var dist : float;
function LateUpdate ()
{
SanityRemover();
}
function SanityRemover() {
dist = Vector3.Distance((CharacterMover.playerPos), (EnemyMovement2.EnemyPos));
print ("Distance: " + dist);
if (CheckWithinRange(dist, 4, 5)) {
SanityValue -= 1 * Time.deltaTime;
}
else if (CheckWithinRange(dist, 3, 4)) {
SanityValue -= 2 * Time.deltaTime;
}
else if (CheckWithinRange(dist, 2, 3)){
SanityValue -= 3 * Time.deltaTime;
}
else if (CheckWithinRange(dist, 1, 2)) {
SanityValue -= 4 * Time.deltaTime;
}
else if (CheckWithinRange(dist, 0, 1)) {
SanityValue -= 5 * Time.deltaTime;
}
print ("Sanity: " + SanityValue);
}
function CheckWithinRange(givenDist : float, min : int, max : int)
{
if(givenDist > min && givenDist < max)
return true;
else
return false;
}