- Home /
Fire Damage 2.0
Hey guys. I may have already asked this question before, but I made a c# script attached to a cube that's attached to a fire that damages my 3d model's health when i go into the fire. It doesn't really work as the player's health isn't going down. I want the box collider's isTrigger on when I enter the fire. The script below uses OnTrigger methods:
using UnityEngine;
using System.Collections;
public class FireDamage : MonoBehaviour { private GameObject _player; private BoxCollider _collider; private Transform _myTransform; private bool _contact = false; private Transform _parent;
// Use this for initialization void Start () { _myTransform = transform;
_collider = GetComponent();
_collider.isTrigger = true;
_player = GameObject.FindGameObjectWithTag("Player");
_parent = transform.parent.transform;
_contact = collider;
}
// Update is called once per frame void Update () {
}
public void OnTriggerEnter(Collider other) { float distance = Vector3.Distance(_myTransform.transform.position, transform.position);
if(distance < 0) { if(_collider) {
_contact = true; _collider.isTrigger = true; PlayerHealth ph = (PlayerHealth)_player.GetComponent("PlayerHealth"); ph.AddjustCurrentHealth(-10);
if(ph.curHealth == 0) { Application.LoadLevel(5);
}
} }
} }
It uses my PlayerHealth script that I mentioned in the other fire damage question. do you think you could help me? thanks
Answer by fafase · Jul 14, 2012 at 06:06 PM
Add a collider to your fire, IsTrigger. Tag the fire with Fire and add the script below to your player. Give values to wait. The purpose of the timer is that you do not want the health to go down every frame as your guy would die almost instantely. With a timer you get hurt every wait second as long as you do not leave the fire.
var timer:float;
var inFire:boolean;
var wait:int;
function Update(){
if(inFire){
if(timer>Time.time){
health-=damage;
timer = Time.time + wait;
}
}
}
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag == "Fire"){
timer = Time.time + wait;
inFire = true;
}
}
function OnTriggerExit(other:Collider){
if(other.gameObject.tag == "Fire")
inFire = false;
}
Your answer
Follow this Question
Related Questions
How to create/fix fire damage script???? 1 Answer
Health Bar fire damage 1 Answer
Damage script is screwed up...? what to do? 1 Answer
Colliders, Triggers, Damage, and Fire 2 Answers
How to damage player when bullet collides with player. 0 Answers