- Home /
Can you help me about collision please ?
Hi every body ! so i have a problem with my game ^^
So i want my player restart the game when he enter in collision I write this and that is good, it is working
using UnityEngine;
using System.Collections;
public class Perso : MonoBehaviour {
Animator anim;
// Use this for initialization
void Start () {
anim = transform.GetComponentInChildren<Animator> ();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D Finish)
{
PersoMort ();
anim.SetTrigger ("mort");
}
void PersoMort () {
Application.LoadLevel (Application.loadedLevel);
}
}**
BUT, i have a floor, the floor have box collider and at first of game the caracter lose and the game restart and i want not that i want he restart the game only when he touch obstacle
Please help me, and thank you for working
Answer by alok-kr-029 · May 06, 2015 at 12:59 PM
What I understand from you is you want to restart the game only when the player hit the obstacle and not when he hit the floor , If I am right you can simply check the name or the tag of the gamobject he collided with like
void OnCollisionEnter2D (Collision2D Finish)
{
if(Finish.gameobject.name == "obstacle" )
{
PersoMort ();
anim.SetTrigger ("mort");
}
}
void PersoMort () {
Application.LoadLevel (Application.loadedLevel);
}
Answer by Landern · May 06, 2015 at 01:01 PM
Thats because anything the player collides with currently is invoking the scripts OnCollisionEnter2D method.
You could name or tag your in scene objects and check to ensure that it's not the floor, or only obstacles.
// Snip
void OnCollisionEnter2D (Collision2D Finish)
{
if (Finish.gameObject.tag == "Obstacle")
{
PersoMort ();
anim.SetTrigger ("mort");
}
}
// Snip
In the above case, assuming the obstacles are tagged "Obstacle" the if statement evaluates whether the game object is tagged obstacle and run your loadlevel and animation, if it's anything else, it will not execute that code block.
Answer by hbalint1 · May 06, 2015 at 01:15 PM
i assume this code is on your player. So when colliding, you should check what have you collided with, and if it's obstacle you can reload.
void OnCollisionEnter2D (Collision2D finish)
{
if(finish.gameObject.Tag == "Obstacle")
{
PersoMort ();
anim.SetTrigger ("mort");
}
}
You can set the Tag in inspector.
Your answer
Follow this Question
Related Questions
check if i'm collided with any objects of a certian tag? 4 Answers
Disabling and Enabling a collision 1 Answer
Unity2D game objects don't collide 1 Answer
How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers
Collision work on pc emulator but not on android device 0 Answers