Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnCollisionStay/Enter - Simulating being hit

Discussion in 'Scripting' started by kittik, Jul 13, 2015.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Hi, I am prototyping an enemy being killed in my game at the moment and I cannot understand why my simple code and collisions are not working harmoniously. Below is the very simple code and screenshots with what I think is important highlighted.

    Code (CSharp):
    1. void OnTriggerStay (Collider other) {
    2.         if(other.gameObject.tag == "fireSpell" || other.gameObject.tag == "lighteningSpell")
    3.         {
    4.             this.gameObject.SetActive(false);
    5.         }
    6.     }





    As you can see, the tags used are correct, there is a collider with is trigger set to true and the projectile is entering, staying and exiting said collider.

    Does the projectile also need a collider with is trigger set to true? I do not see why that would be the case. A fix and an explanation, so that I don't make whatever mistake I am making would be nice.
     
  2. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Note: I have just changed OnTriggerStay to both OnCollisionEnter and OnCollisionStay and it is still not functioning.
     
  3. kemar

    kemar

    Joined:
    Nov 4, 2014
    Posts:
    15
    Hi,

    Add a Rigidbody componenent to your object.
     
  4. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Hey Kemar,

    What reasoning do you have for making the object a Rigidbody? Aside from letting the target fall to the floor, it will not achieve anything purposeful (yes, I have tried doing it). I have now added colliders (triggered and otherwise) to my projectiles as well as the targets - still no success.
     
  5. kemar

    kemar

    Joined:
    Nov 4, 2014
    Posts:
    15
    kittik likes this.
  6. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    The projectiles and now the target have Rigidbodies (with is Kinematic as false set) as you suggest and the target is falling over when hit. I am not entirely sure what is wrong here. Would some more screenshots assist?
     
  7. kemar

    kemar

    Joined:
    Nov 4, 2014
    Posts:
    15
    1) An object (FireSpell) with a rigidbody where gravity and kinematic is on false
    Screen1.png
    2) Another object with a rigidbody where gravity and kinematic is on false.
    I put the script on this object for handle the collision

    3) Here the script


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class collScript : MonoBehaviour {
    5.  
    6.     void OnCollisionEnter  (Collision other) {
    7.         if(other.gameObject.tag == "fireSpell" || other.gameObject.tag == "lighteningSpell")
    8.         {
    9.             Debug.Log("Collision");
    10.             this.gameObject.SetActive(false);
    11.         }
    12.     }
    13. }

    Sorry for my bad English
     
    kittik likes this.
  8. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Brilliant, it all works! Thank you very much.
     
    kemar likes this.