In my FPS game the player has a script attached to him that handles hitpoints:
var PlayerHP = 3;
function OnTriggerEnter(Projectile2 : Collider) {
if (Projectile2.CompareTag ("EnemyProjectile"))
--PlayerHP;
if (PlayerHP == 0)
Destroy (gameObject);
}
Projectile2 represents the enemy projectiles. Though they clearly collide with the character controller, the player is not being destroyed after 3 hits. Can I get some help on this?
Have you put a Debug.Log line in to make sure OnTriggerEnter is being called, and that the tag comparisons are working and the Player’s health is being decremented?
Unsure of what you mean by putting in a Debug.Log line. I looked it up in the scripting reference but that doesn’t seem to help. The player’s health is apparently not being decremented. And how would I know if the tag comparisons were working?
Make a ‘Debug.Log(“These tags work”);’ When it goes…
Like this?:
var PlayerHP = 3;
function OnTriggerEnter(Projectile2 : Collider) {
if (Projectile2.CompareTag ("EnemyProjectile"))
Debug.Log("These tags work");
--PlayerHP;
if (PlayerHP == 0)
Destroy (gameObject);
}
Debug.Log(Object obj) Will just output the object to the console when your code reaches that line. It is useful for many debugging purposes. In this case, you can use it to determine if your code is reaching a certain point.
So if you wrote
var PlayerHP = 3;
function OnTriggerEnter(Projectile2 : Collider) {
Debug.Log("On Trigger Enter");
if (Projectile2.CompareTag ("EnemyProjectile"))
{
Debug.Log("Player Hit!");
--PlayerHP;
}
if (PlayerHP == 0)
Destroy (gameObject);
}
and ran the code, and the console printed out:
On Trigger Enter
Player Hit
Then you would know that OnTriggerEnter is being called, and the compareTag() is working, and the player’s health is getting decremented.
but if you ran the code, and the console only printed:
On Trigger Enter
Then you know OnTriggerEnter is being called but compareTag() is not working. And of course, if the console prints nothing, then OnTriggerEnter is not being called.
If OnTriggerEnter is not being called make sure that both the Player and Projectile have colliders, one of them has isTrigger checked, and one of them has a rigid body attached. In this case I would probably attach a rigid body to both of them, but the Projectile for sure.
If OnTriggerEnter is being called and the compareTag is not working make sure that the custom tag you made is assigned to the Projectiles, and is spelled exactly right.
Nothing shows up in the console. I think I know why. The player needs to be a trigger, which it isn’t right now because it only has a Character Controller. So apparently I need a collider and a character controller?
Edit: I tried adding a capsule collider with a trigger. No change.
bump
Anyone have any ideas on this?
Sorry for bumping a second time, but I have tried multiple things and it still fails to work. it is very important that I get an answer on this as soon as possible.
It really is absolutely vital that I get help on this ASAP. If anyone has any ideas as to what is going on, please advise.
Nevermind! I wrote a new script that does the job perfectly. Thanks for the help.
(Did I just quintuple post? I stand in disgrace.)