- Home /
Trigger2D not working
Hello Guys, my first question in the forum, so let's go on. I'm having problems to put my player in receiving damage from the FireBall. I'm using onTriggerEnter2D function to take the Player to GameOver screen, so, i thought that if that happens, i would overwrite the function to take the fireball damage. but it doesnt work. Besides that, i tried using onCollisionEnter2D and my player goes through the fireball.
so here it is the code that works: public class BolaFogo : MonoBehaviour {
void OnTriggerEnter2D (Collider2D colisor)
{
if (colisor.gameObject.tag == "Player")
{
Application.LoadLevel ("gameover");
}
}
void Start () {
player = GameObject.FindGameObjectWithTag ("Player").transform;
}
void Update () {
}
}
and now this is what i'm trying to do that isn't working.
public class BolaFogo : MonoBehaviour {
public Rigidbody2D PlayerRigidbody;
private Transform player;
PlayerHealth playerhealth;
void OnTriggerEnter2D (Collider2D colisor)
{
if (colisor.gameObject.tag == "Player")
{
playerhealth.TakeDamage(35);
}
}
and now i'll put my class about player Health
public class PlayerHealth : MonoBehaviour {
public int startingHealth = 100;
public Slider healthSlider;
public int currentHealth;
bool damaged;
bool isDead;
public void TakeDamage (int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
if (currentHealth <= 0 && !isDead)
{
Death();
}
}
void Death()
{
isDead = true;
}
}
sorry for the mess and my english. Please consider helping me Guys, my teacher tried to help me, we saw in the class that the method to take damage is working well, otherwise, it doest collide with the fireball, i'm in seriously doubt what do i have to use, if it it EnterOnCollision2d or EnterOnTrigger2D.
when i use onTriigger2d, it appears an error about set instance to object, when i use onCollision2d, nothing happens, my player just go through the fireball
what about it isn't working? any error messages? if so, post the complete message
All of you, I made it !!
I'll send my Bola Fogo Class, the problem was in the colliding.
using UnityEngine;
using System.Collections;
public class BolaFogo : $$anonymous$$onoBehaviour {
public Rigidbody2D PlayerRigidbody;
//teste
bool playerInRange;
public int attackDamage = 10;
GameObject playerobj;
private Transform player;
private float posicaoY = 0f;
PlayerHealth playerhealth;
void OnTriggerEnter2D (Collider2D colisor)
{
if (colisor.gameObject.tag == "Player")
{
playerInRange = true;
}
}
// Use this for initialization
void Start () {
playerobj = GameObject.FindGameObjectWithTag ("Player");
playerhealth = playerobj.GetComponent<PlayerHealth> ();
player = GameObject.FindGameObjectWithTag ("Player").transform;
Destroy (gameObject,3.5f);
PlayerRigidbody.AddForce (transform.up * 500f);
posicaoY = transform.position.y;
}
// Update is called once per frame
void Update () {
if (posicaoY > transform.position.y) { //Descendo
transform.eulerAngles = new Vector2(180, 0);
}
posicaoY = transform.position.y;
if (playerInRange){
Attack();
Destroy(gameObject);
}
}
void Attack (){
if (playerhealth.currentHealth > 0) {
playerhealth.TakeDamage (20);
}
}
}
Your answer
Follow this Question
Related Questions
*** Trigger Collider2D event only once *** 3 Answers
Creating 'redstone' like wires 1 Answer
2D collision not registering 1 Answer
Problem with collision with two colliders simultaneously in Unity 2D 1 Answer
Unity 2D Collider Safe Sizes 0 Answers