- Home /
Collision without movement!??
Hi all! I have created a game which is quite a simple thing. A ball rolls along the ground and if you collide with something, the ball shoots off to the side and off the edge of the board. But I'm having a major problem, in that when I start the game, the ball automatically bumps off the side of the board. For some reason it is detecting that it has already collided with something, even though there has been no collision because the ball isn't even moving!
Here's the area that detects the collision:
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.name.Contains("Ground"))
{
return;
}
if (collisionInfo.collider.name.Contains("Block"))
{
Instantiate(explosionEffect, transform.position, transform.rotation);
GameObject impactGO = Instantiate(explosionEffect);
Destroy(impactGO, 5f);
Destroy(collisionInfo.gameObject);
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
}
I'm hoping there's something simple in my coding above that's causing the error, because if it isn't in this part then it's probably somewhere else and I'll never find it...
Answer by Legend_Bacon · Mar 26, 2018 at 11:48 AM
Hello there,
OnCollisionEnter in Unity is called on Start() automatically, whether the object is moving or not. There's nothing wrong with your code here, but I am guessing that you start the game with your ball touching an object that has "Block" in its name? I'd suggest throwing a Debug.Log(collisionInfo.collider.name);
in there to see which object that is.
Hope that helps!
Cheers,
~LegendBacon
Answer by Jacebook · Mar 26, 2018 at 11:49 AM
Okay I've managed to fix it myself. I have no idea what was causing it, but I've put the entire Void's code inside this:
if (rb.transform.position.z > 10) {
<all code in here>
}