Continuously check if not colliding.

Hey, I have those lines in one of my scripts:

function OnTriggerExit(collision : Collider){ 
colliderCollided = false;
weaponManager.GetComponent("weaponManager").colliderCollided = false;
}

function OnTriggerStay(collision : Collider){ 
colliderCollided = true;
weaponManager.GetComponent("weaponManager").colliderCollided = true;
}

The idea is to have a basic check for if my game object is colliding with something or it’s not. This however is not so efficient as OnTriggerExit is called only once when the trigger is not touching a collider. How can I check continuously if my trigger is not colliding with anything?

private var collisionCount = 0;

function OnCollisionEnter () {
	collisionCount++
}

function OnCollisionExit () {
	collisionCount--;
}

If collisionCount is 0, it’s not colliding with anything.

–Eric

2 Likes

If the object is destroyed while colliding, OnCollisionExit will NOT be called. Just something to be aware of.

That’s true…to take care of that, I’ve done this before:

function DestroySelf () {
	transform.Translate (Vector3.up * 10000);
	yield WaitForFixedUpdate();
	Destroy (gameObject);
}

Instead of calling Destroy directly, that moves the object out of the way in order to give OnCollisionExit the ability to fire first.

–Eric

What if I try jump next to a wall?

I’m trying this and it’s saying “function could not be found”. I know this is old so it’s probably some version issue, but how would I fix this?

This is so old it’s javascript, it’s a different programming language that is no longer supported in Unity (and good riddance I say).

Equivalent code in C# would be:

private int collisionCount = 0;
private void OnCollisionEnter() {
   collisionCount++;
}
private void OnCollisionExit() {
   collisionCount--;
}
public class EditorTest : MonoBehaviour
{
    Collider _collision;

    private void FixedUpdate()
    {
        if(_collision == null)
        {
            Debug.Log("No trigger.");
        }
        else
        {
            Debug.Log("Trigger");
            _collision = null;
        }
    }

    private void OnTriggerStay(Collider other)
    {
        _collision = other;
    }
}

This would fix the OnCollisionExit isssue if object is destroyed. Would need some changes if more of one collision is triggering at same time and you need to iterate though each collision objet but for only one collision it works.
Btw good necromancy.

Oh wow thanks! I never would’ve guessed this.

Hi, sorry to bother you. I’m a little confused on this code. I can’t figure out why this isn’t working. It’s probably some logic thing I’ve messed up, could you check it out? I’ve tried swapping collision enter and collision exit and that doesn’t work. Do I need to make sure it’s tagged with the ground? Anyways here’s the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public Rigidbody rb;
    public int collisoncount = 0;
    public float forward = 750f;
    public float sideways = 500f;
    public float jump = 150f;
    public int collisioncount = 0;

   
    
        void  FixedUpdate () {


        rb.AddForce (0, 0, forward * Time.deltaTime);

   
        if (Input.GetKey ("d")) {
            rb.AddForce (sideways * Time.deltaTime, 0, 0);
        }

        if (Input.GetKey ("a")) {
            rb.AddForce (-sideways * Time.deltaTime, 0, 0);
        }
        if (collisioncount == 0) {
            if (Input.GetKeyDown ("space")) {
                rb.AddForce (0, 100, 0);
            }

        }
           
            }
    private void OnCollisionExit() {
        collisioncount++;
    }
    private void OnCollisionEnter() {
        collisioncount--;
    }
        }

Check your OnCollisionExit and Enter methods. Unity detects as message OnCollisionExit(Collision collision) with a collision parameter… yours are empty, so Unity does not take nothing and your OnCollisionExit and OnCollisionEnter are just two private methods never called from anywhere.

    //Change this
    private void OnCollisionExit() {
        collisioncount++;
    }
    private void OnCollisionEnter() {
        collisioncount--;
    }

    //To this
    private void OnCollisionExit(Collision collision) {
        collisioncount++;
    }
    private void OnCollisionEnter(Collision collision) {
        collisioncount--;
    }

Thank you so much!