OnCollisionStay/Enter - Simulating being hit

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.

void OnTriggerStay (Collider other) {
        if(other.gameObject.tag == "fireSpell" || other.gameObject.tag == "lighteningSpell")
        {
            this.gameObject.SetActive(false);
        }
    }




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.

Note: I have just changed OnTriggerStay to both OnCollisionEnter and OnCollisionStay and it is still not functioning.

Hi,

Add a Rigidbody componenent to your object.

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.

Because you need it for the detection

“Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.”

1 Like

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?

  1. An object (FireSpell) with a rigidbody where gravity and kinematic is on false

  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

using UnityEngine;
using System.Collections;

public class collScript : MonoBehaviour {

    void OnCollisionEnter  (Collision other) {
        if(other.gameObject.tag == "fireSpell" || other.gameObject.tag == "lighteningSpell")
        {
            Debug.Log("Collision");
            this.gameObject.SetActive(false);
        }
    }
}

Sorry for my bad English

1 Like

Brilliant, it all works! Thank you very much.

1 Like