- Home /
Making a separate question for part 2, as I really need help solving it.
Alternative collision detection methods? (c#)
I have a scene where I have several objects colliding, often even overlapping each other (I want this), and I've got the code performing pretty much the way I want, with simply
void OnTriggerEnter(Collider other)
{
colliding = true;
}
void OnTriggerExit(Collider other)
{
colliding = false;
}
But now I've run into two snags.
EDIT: SOLVED. Look at the comment below. OnTriggerExit mostly works, but not always. When I move one of the colliding objects away too fast, especially when it was just touching and not overlapping, this function never fires off, and the object is stuck in a perpetual "colliding" state. Is there any way to make it more... sensitive?
How can I differentiate between when objects are just touching and overlapping?
Additionally, the collision sphere thing won't do here, as all my objects are rectangularly shaped. Help?
Right so, problem 1 is gone, did it with this code.
public bool colliding = false;
private int collNum = 0;
void Update ()
{
if(collNum == 0)
colliding = false;
if(collNum > 0)
colliding = true;
if(collNum < 0)
{
Debug.LogError(this.gameObject + " Undetected collision exit");
collNum = 0;
}
}
void OnTriggerEnter(Collider other)
{
collNum++;
}
void OnTriggerExit(Collider other)
{
collNum--;
}
This worked, because my problem was that my objects were often colliding with several other objects at the same time. So enters and exits were getting fired off willy nilly. $$anonymous$$eeping count fixed that. So far the error has not been fired off even once.
$$anonymous$$y 2nd question is still valid though.
Answer by SpecticalPro · Jun 26, 2013 at 07:16 PM
try checking distance from the center of the objects rather than OnTriggerExit()
ie. subtract the two transforms and get the squared magnitude should work
Then each object would have to look at every other object in the scene to check for collisions... Is that wise? We're talking thousands of objects here.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Zero collision responsiveness - C# 1 Answer
Distribute terrain in zones 3 Answers
How to detect an object inside another object? 1 Answer
Yet another null reference exception 1 Answer