Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by BeastGames · Aug 13, 2016 at 07:58 AM · collisioncollision detectionendgame

How can I end and reset the game when 'Ball' collides with 'WallLeft'?

I am making a pong-type game. I want the game to end when 'Ball', collides with 'WallLeft'. I have tried many codes and scripts from my own knowledge, tutorials, forums, and unity answers but nothing I have tried has worked. Please help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Denis115 · Aug 13, 2016 at 03:01 PM

Make sure your ball and wall have fitting colliders. For a 3D game a Sphere Collider for the ball and a Box Collider for the wall, for a 2D game a Circle Collider 2D and Box Collider 2D. Also make sure you have a game over scene in your game, if not create one.

Here you can see 2 cubes that i used for the wall and ball, and a script called "Test". Create a script like this (You didn't specify what language i use, i will show you in C#): https://s10.postimg.org/h7vrnw11l/image.png

Add a Rigidbody component (or rigidbody 2D) to the Wall object, and make sure that 'Use Gravity is not ticked. (https://s10.postimg.org/8qw9cywcp/image.png)

Add a Rigidbody component to the ball as well and make sure that 'Is Kinematic' is ticked. (https://s10.postimg.org/pfxp8vsy1/image.png)

You can easily detect the Wall by adding a tag to it. Select the wall object, go to Tag > Add tag, click the plus icon and type in Wall (https://s10.postimg.org/5mllg6fk9/image.png)

Now with the wall selected, go to tag, and select Wall (https://s10.postimg.org/cec0j14jt/image.png)

Now open the script you created earlier, and before doing anything, make sure you add using UnityEngine.SceneManagement; at the top of your script.

Now put this code in:

 void OnCollisionEnter(Collision exampleCol) {
         if(exampleCol.collider.tag == "Wall")
         {
             //Replace 'Game Over' with your game over scene's name.
             SceneManager.LoadScene("Game Over");
         }
     }

What this means is when the ball collides with something, if its tag is "Wall" , load the Game Over scene. And since we gave the wall object and only the wall object the "Wall" tag, the SceneManager.LoadScene code will only run when the ball collides with the wall.

Before this can work you need to add your scenes to the build settings. Go to file > build settings, drag all your scenes to the list, and make sure the order is correct. The main scene should be first in the list, and then the game over scene. (https://s10.postimg.org/hamohnk89/image.png)

Now attach the script to the ball and press play!

Additionaly, since you said you wanted 'the game to end when it collides with WallLeft', you could completely quit the game with this code:

 void OnCollisionEnter(Collision exampleCol) {
             if(exampleCol.collider.tag == "Wall")
             {
                 Application.Quit();
             }
         }

The above code obviously won't work in the editor, but if you built the game and ran it, when this code ran, the game would've completely closed.

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
0

Answer by LK84 · Aug 13, 2016 at 10:39 AM

Depending on how you define "end and reset game" (just loading a scene?) you can just use OnCollisionEnter (drop this script to your Ball game Object):

 void OnCollisionEnter(Collision wall) 
 {
 if (wall.gameObject.name=="WallLeft")
 {
 //End and Reset Game whatever you need to do
 }
 }

Note, it's better to use tag insted of name, so you might wanna tag your WallLeft GameObject

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

6 People are following this question.

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

Related Questions

Changing light color between players when they collide (BOLT) 0 Answers

Why Character is going through the floor. 0 Answers

Problem with collision 1 Answer

Charachter Controller and collisions (Door) help 1 Answer

How i prevent 2 obejcts with kinematic checked in both rigidbodies collide? 3 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