- Home /
Damage rate HELP
Hi!
I'm working on a damage script for the player, but I'm having a problem. Now when you walk on a specific trigger your player loses health and that works like it should. But what I want to implement is that when you stay in the trigger box you should lose health every second or two.
I've tried OnTriggerStay, but it wont work like I want it to. I think it calls it every frame, so the player loses all his health in like 1 second.
Here's my script:
var damage = 10;
var healthScript : Health;
var damageSound: AudioClip;
//Disable objects
var pauseScript : Pause;
var deathScreen : boolean;
var deathSound: AudioClip;
var PlayerObjects : GameObject;
var flashlightObjects : GameObject;
var cameraObjects : GameObject;
var interactionObjects : GameObject;
function OnTriggerEnter ()
{
healthScript.healthPoints -= damage;
AudioSource.PlayClipAtPoint(damageSound, transform.position);
//Death
if (healthScript.healthPoints <= 0)
{
AudioSource.PlayClipAtPoint(deathSound, transform.position);
healthScript.healthPoints = 0;
deathScreen = true;
Time.timeScale = 0;
AudioListener.pause = true;
Screen.showCursor = true;
Screen.lockCursor = false;
//Disable components
PlayerObjects.GetComponent(CharacterMotor).enabled = false;
PlayerObjects.GetComponent(SmoothMouseLook).enabled = false;
cameraObjects.GetComponent(SmoothMouseLook).enabled = false;
PlayerObjects.GetComponent(Footsteps).enabled = false;
PlayerObjects.GetComponent(Crosshair).enabled = false;
flashlightObjects.GetComponent(Flashlight2).enabled = false;
PlayerObjects.GetComponent(KeyInventory).enabled = false;
PlayerObjects.GetComponent(BatteryInventory).enabled = false;
PlayerObjects.GetComponent(HealthInventory).enabled = false;
pauseScript.enabled = false;
interactionObjects.SetActive(false);
Destroy(gameObject);
}
}
function OnTriggerStay ()
{
healthScript.healthPoints -= damage;
if (healthScript.healthPoints <= 0)
{
AudioSource.PlayClipAtPoint(deathSound, transform.position);
healthScript.healthPoints = 0;
deathScreen = true;
Time.timeScale = 0;
AudioListener.pause = true;
Screen.showCursor = true;
Screen.lockCursor = false;
//Disable components
PlayerObjects.GetComponent(CharacterMotor).enabled = false;
PlayerObjects.GetComponent(SmoothMouseLook).enabled = false;
cameraObjects.GetComponent(SmoothMouseLook).enabled = false;
PlayerObjects.GetComponent(Footsteps).enabled = false;
PlayerObjects.GetComponent(Crosshair).enabled = false;
flashlightObjects.GetComponent(Flashlight2).enabled = false;
PlayerObjects.GetComponent(KeyInventory).enabled = false;
PlayerObjects.GetComponent(BatteryInventory).enabled = false;
PlayerObjects.GetComponent(HealthInventory).enabled = false;
pauseScript.enabled = false;
interactionObjects.SetActive(false);
Destroy(gameObject);
}
}
Does anyone know how to fix this?
Answer by Alessio89 · Aug 20, 2014 at 11:16 AM
It's actually pretty easy: you can either use a timer like so:
var damageTimer : float;
function OnTriggerStay ()
{
damageTimer += Time.deltaTime;
if (damageTimer > [theDelayYouWantHere])
{
healthScript.healthPoints -= damage;
damageTimer = 0;
}
}
Or you can use a CoRoutine. Now I'm not familiar with how CoRoutine are handled in javascript, but you can check the documentation for that. If I recall correctly, it's easier in Javascript than it is in C# to use coroutines. :)
Here's the link to the docs: CoRoutines
One more question! :) A problem I have now is when I walk in the trigger a damage sound plays + the OnTriggerStay sound also. So it plays these two sounds the same time so it sounds kinda weird. Do you know how to prevent this? I want a sound playing when you hit it, then when you stay in the trigger the sound keeps playing.
function OnTriggerEnter ()
{
healthScript.healthPoints -= damage;
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
function OnTriggerStay ()
{
damageTimer += Time.deltaTime;
if (damageTimer > 1)
{
healthScript.healthPoints -= damage;
damageTimer = 0;
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
}
function OnTriggerExit ()
{
damageTimer = 0;
}
$$anonymous$$ake sure that the damageTimer is set to 0 before entering the trigger.
Also you could keep a bool to check if the player has entered just now the trigger. You can do this: keep a bool entered and set it to true in the OnTriggerEnter. You can then check in OnTriggerStay if this bool is true. If it is you just walked into the trigger, so don't play the damage sound. You then start counting with the timer, at the end of the timer you just check again if entered is true. If it is, set it to off again. Something like this:
var firstStepInTrigger = false;
function OnTriggerEnter ()
{
firstStepInTrigger = true;
healthScript.healthPoints -= damage;
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
function OnTriggerStay ()
{
damageTimer += Time.deltaTime;
if (damageTimer > 1)
{
healthScript.healthPoints -= damage;
damageTimer = 0;
if (firstStepInTrigger)
{
firstStepInTrigger = false;
}
else
{
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
}
}
function OnTriggerExit ()
{
damageTimer = 0;
firstStepInTrigger = false;
}
Well it works, kinda. When I just walk on it it works, when I stay on it, the first damage sequence doesn't have any sound, but the rest of them have it. Do you know why? Sorry for all the questions, I'm really grateful that you're helping me! :)
try to replace
if (firstStepInTrigger)
{
firstStepInTrigger = false;
}
else
{
AudioSource.PlayClipAtPoint(damageSound, transform.position);
}
with
if (firstStepInTrigger)
{
firstStepInTrigger = false;
}
AudioSource.PlayClipAtPoint(damageSound, transform.position);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with OnTriggerEnter() 1 Answer
Damage problem 4 Answers
OnTriggerStay/Enter/Exit 2 Answers
Problem with enemy health. 1 Answer