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?
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.
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.