Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JJNCreator · Jul 14, 2012 at 04:55 PM · playerdamagefireistriggerhealth

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges