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 Jaidan · Feb 08, 2015 at 07:19 PM · c#2dplatformerlose

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

}

Comment
Add comment · Show 1
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 Darsh2019 · Feb 09, 2015 at 08:36 AM 0
Share

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..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 .

Comment
Add comment · Show 1 · 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 Jaidan · Feb 09, 2015 at 05:08 PM 0
Share

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

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make character stop at wall? 1 Answer

Way to have 2D Sprites lit by Lights? 3 Answers

Unity 2D Platformer Enemy follow Player on X-axis only, c# 1 Answer

Unity 2D Platfomer Enemy follow player only on x-axis, c# 1 Answer

Multiple Cars not working 1 Answer


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