Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by samiiii · May 06, 2015 at 12:47 PM · unity 5javascriptcollisioncollider

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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);
  }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges