- Home /
Player damage stops working over time
Here is the code for when the enemies attack the player in my simple chase-the-player game:
var victim : Transform;
var dieRange = 1.5;
var explosion : GameObject;
private var controller : CharacterController;
function Start()
{
controller = GetComponent(CharacterController);
}
function Update()
{
var range = Vector3.Distance(victim.position, transform.position);
if (range <= dieRange)
{
playerHP.healthPoints=playerHP.healthPoints-0.3;
if(Random.value>0.85)
{
Instantiate (explosion, transform.position, transform.rotation);
}
}
}
I'm having two major problems with this so far:
1) The main character takes damage, but after a short amount of time (about five seconds) the health will never go down anymore, so it will be stuck at a value like 47% or 56% and not go down. The enemies continue to chase and surround the player but nothing else happens.
2) I'm getting a recurring error on the command console window saying: "The variable victim of 'damagePlayer' has not been assigned." This is completely untrue because I have declared it both in the script and for each of the AI characters, so I have no idea what is going wrong here. (FIXED)
One of the other scripts I use is the one which modifies the text in the GUI to represent what health the player is at.
static var healthPoints: int = 100;
var targetGuiText: GUIText;
function Start () {
}
function Update () {
targetGuiText.text = "Health: "+(healthPoints)+"%";
}
On a lesser note, when my First Person Character jumps, he flies off into the atmosphere and doesn't come back down. I'm using the default FPS character and have not touched the jumping mechanics whatsoever.
Probably worth noting that I'm using the A* Pathfinding Project to make the enemies head towards the player, I'm on a bumpy terrain and I've got eight enemies going at once.
It's working now, thanks for the help!
Concerning your victim variable, have you dragged the object in the inspector? or place this in the start function:
victim = GameObject.Find("NameOfTheObject").transform;
I suspect it is something in the victim's script
by the way the line playerHP.healthPoints=playerHP.healthPoints-0.3; can be shortened:
playerHP.healthPoints-=.03;
just FYI
once the error begins, hit the pause button, and select your various enemies. you will find the victim var is empty. Then select your victim and see what's wrong with him... maybe you instance a new enemy? or disable/destroy the player?
as for the jumping, is there a var for gravity in the script; or jumpheight? it may just need the tweaking of vars, but I'm not familiar with the script offhand
$$anonymous$$mm, would there be in the PlayerHp script some kind of
if(healthPoints<=0) Destroy(gameObject);
That could explain the health not going down anymore and the loss of the victim transform. I think that showing the other scripts involved would fix this real quick.
You may want to cache the Transform in this script as well.
As @Fafase says, I suspect the Victim has 'died' in some way, or are you instantiating enemies who's victim is null and needs hooking up when they are instantiated?
@$$anonymous$$Fili the same way you cache the controller, you can just add:
var my_transform : Transform;
To the local variables and then in Start() or Awake() you would do:
my_transform = transform.
I am a C# bod so I can't be sure if the above JS is correct, but it probably is.
I've never tried it but transform is probably very like GetComponent(Transform)
Your answer
Follow this Question
Related Questions
Stay Back! 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Object2Terrain error/wont work. 1 Answer
Taking a hit 3 Answers
How To Make Enemy Chase You? 1 Answer