- Home /
Simple collision detection C# 2D
Hello, i have looked all around the internet looking for answers, but i am a beggininer and dont understand some of the stuff. Could someone help me with some code to detect a simple collision between my character and the environment. If the character collides with the environment then i need my "Loose" menu to pop down, which i already have animations for. I am also going to have the script attached to a manager component i have already
So basically:
If character collides with environment Loose menu pops down
Thanks, here is some code i have done already:
using UnityEngine; using System.Collections;
public class IfLoose : MonoBehaviour {
bool AudioListener.pause;
public GameObject looseMenuPanel;
private Animator anim;
void Start () {
Time.timeScale = 1;
anim = looseMenuPanel.GetComponent<Animator>();
anim.enabled = false;
}
public void OnCollisionEnter(Collision collision){
}
public void Update () {
}
public void PauseGame(){
anim.enabled = true;
anim.Play("PauseMenu");
Time.timeScale = 0;
AudioListener.pause = true;
}
}
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionEnter2D.html check this link.. add Collider2D to environment , trigger off & give tag to environment as shown in link..
Answer by shriya · Feb 09, 2015 at 09:34 AM
HI, In the method On CollisionEnter method do the following code
public void OnCollisionEnter(Collision collision)
{
StartCoroutine("PauseGame");
}
Your character should have rigidbody and a collider attached. Also if you have added tags ,then you need to check it in CollisionEnter Code as mentioned in the link suggested by Darsh2019. And this script should be added to the player .
Is there a way i can have a script with the code you have, but then i would have to call the Coroutine from another script, how would i do this because i get a error saying coroutine could not be started. The routine in the other script is public