My collision is not being detected or does not produce the desired outcome.
I want to detect a collision between two gameObjects: "JoaoBola" and "Spikes", and, once detected, I want to destroy the gameObject "JoaoBola". In order to do so, I attached the following script to JoaoBola:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeathBySpikes : MonoBehaviour {
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "Spikes") {
Destroy (gameObject);
}
}
}
But it is not working. JoaoBola does collide with Spikes but it does not "die". I do not understand why I am not getting the expected behavior. Useful informations: Both JoaoBola and Spikes have a rigidbody2D and a collider. JoaoBola is dynamic and Spikes is kinematic.
Answer by MrCrumbl3d · May 05, 2017 at 04:23 PM
Try this.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DeathBySpikes : MonoBehaviour {
public GameObject JoaoBola;
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "Spikes") {
Destroy (JoaoBola);
}
}
}
Answer by HelloTom · May 05, 2017 at 04:30 PM
If the gameobject is moving really fast then you might want to change the collision detection from discrete to continuous or continuous dynamic.
Your answer
Follow this Question
Related Questions
Physics 2D with tile collider corner problem 2 Answers
2D sprite won't destroy on collision 0 Answers
C# - My increament code is not working? 1 Answer
Checking collision before instantiating prefab 0 Answers
OnCollisionEnter Problem 1 Answer