Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 brenolimaa96 · Dec 01, 2015 at 02:39 PM · collisioncollider2d gametrigger2d-physics

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

Comment
Add comment · Show 2
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
avatar image gjf · Nov 30, 2015 at 06:36 PM 0
Share

what about it isn't working? any error messages? if so, post the complete message

avatar image brenolimaa96 gjf · Dec 01, 2015 at 10:14 AM 0
Share

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);
         }
     }
 
 
 }
 

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

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


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