- Home /
Question by
rautudenis · Apr 17, 2020 at 09:57 AM ·
c#collision3d
OnCollisionEnter bug
Hi, Sometimes when the player is on collision with an obstacle, the script is called twice.
public class Collision : MonoBehaviour
{
public Move move;
public Jump jump;
public GameObject player;
public bool check = false;
[SerializeField] private RectTransform replayTxtTrans = null;
[SerializeField] private RectTransform hiscoreTxtTrans = null;
[SerializeField] private RectTransform endGameTxtTrans = null;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.gameObject.tag == "Obstacle")
{
move.enabled = false;
jump.enabled = false;
Vibration.VibratePop();
player.GetComponent<Rigidbody>().isKinematic = true;
player.transform.GetChild(1).gameObject.SetActive(false);
player.transform.GetChild(0).gameObject.SetActive(true);
replayTxtTrans.gameObject.SetActive(true);
LeanTween.scale(replayTxtTrans, new Vector3(1f, 1f, 1f), 0.3f).setDelay(0.1f).setEaseInOutBack();
endGameTxtTrans.gameObject.SetActive(true);
hiscoreTxtTrans.gameObject.SetActive(false);
FindObjectOfType<AudioManager>().Play("playerdeath");
check = true;
Debug.Log(check);
}
}
}
In the console he shows this:
Why? Thanks for help.
untitled.png
(69.9 kB)
Comment
Answer by Quickz · Apr 17, 2020 at 02:06 PM
You might have a second Collider component on the Player itself or a child object.
I don't have, it doesn't always do that. Thanks for answer
Answer by GreatOscarF · Apr 17, 2020 at 10:15 PM
Maybe it's something to do with your obstacle, like it has two parts and the second part only sometimes hits....
Your answer
![](https://koobas.hobune.stream/wayback/20220612231700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collision problem 4 Answers
GJK box problem 0 Answers
Multiple Cars not working 1 Answer