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 neblitor · May 17, 2014 at 04:34 AM · namingcollsion

specifying objects onCollisionEnter?

i have a quick question which will be super easy to answer. im currently making a infinate runner type game for a class project and i need some help with collsions. the game is setup as a 2d platform where a character is running (or made to look like there running with a scrolling texture) through a infinite tunnel, the player has to avoid obstacles similar to flappy birds using jumps. The script i'm using at the moment is setup so that when the player collieds with obstacles the game will reload.

the issue: the issue is that the code ive used apples to all collsions so when the player hits the ground platform the game reloads

what i want to happen: i want the character to be able to run on a platform and die when he hits the a specific obstacle.

so im think i need to specify a gameobject to collide with?

any help would be great :D thanks heaps

this is my current code:

 // Update is called once per frame
 void Update ()
 {
     // Jump
     if (Input.GetKeyUp("space"))
     {
         rigidbody2D.velocity = Vector2.zero;
         rigidbody2D.AddForce(jumpForce);
     }
     
     // Die by being off screen
     Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
     if (screenPosition.y > Screen.height || screenPosition.y < 0)
     {
         Die();
     }
 }
 
 // Die by collision
 void OnCollisionEnter2D(Collision2D other)
 {
     Die();
 }
 
 void Die()
 {
     Application.LoadLevel(Application.loadedLevel);
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rgowen · May 17, 2014 at 05:35 AM

The onCollisionEnter() function gets passed a Collision2D object, with which you can find out what GameObject it is hitting:

 // Die by collision
 void OnCollisionEnter2D(Collision2D other)
 {
     if(other.collider.gameObject == deadlyGameObject) 
     {
         Die();
     }
 }

You might need to tweak the evaluation in the if statement, but that's the principle.

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 robertbu · May 17, 2014 at 05:46 AM 0
Share

Typically you have a group of items you want to be deadly. Ins$$anonymous$$d of checking against a specific game object, you check the tag. So line 4 of @$$anonymous$$wen's code would be:

 if (other.collider.tag == "Deadly") 

https://docs.unity3d.com/Documentation/Components/class-Tag$$anonymous$$anager.html

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

[Question about naming convention] What does the hash tag in front of game object names usually mean? E.g."#GameObjectName" 0 Answers

Make transparent 3d object with collision detection 3 Answers

Inspector Property Names Documentation.. 1 Answer

How to drag around an object and making it collide with other object 2D 2 Answers

How do I name cloned objects inside a for-loop? 2 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