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 /
avatar image
0
Question by Kimbodja · Apr 18, 2018 at 10:34 AM · 2d2d game2d-physics

,Start and Game over

Hi!

I am super new to Unity and trying to create my first project and I have no idea how to start or end the game at this point. The game has one character that is clickable/touchable, every click/touch generetes a point. As of right now when I press the "play" button in Unity the character falls from the middle of the screen down and then starts to bounce around, I've set it up this way with Rigidbody and box colliders.

I want to have the character frozen in place until i click/touch him, then he begins to fall. I also want to end the game whenever I click/touch the background, as in everything else except the character. When that happens I want the score saved and then just simply add a "Retry" button. Does that make any sense? Could someone point me in the right direction? :)

/Kim

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
1

Answer by Kciwsolb · Apr 18, 2018 at 08:57 PM

In your code, get a reference to the Rigidbody on the character in Awake. Then in Start call a function that enables the kinematic property of the rigidbody. A rigidbody marked as kinematic will not move except by code (read more here).

 private Rigidbody rigidbody;
 
 private void Awake()
 {
         rigidBody = GetComponent<Rigidbody>();
 }

 private void Start()
 {
         ResetGame();
 }

 public void ResetGame()
 {
         rigidbody.isKinematic = true;  //Character is prevented from falling
 }

 public void StartGame()
 {
         rigidbody.isKinematic = false; //Character will start to fall
 }

Now, you will need another script that gets the mouse position and checks for a hit on a collider when you click (there are plenty of examples of this so just do a quick search). Once you have that script, use the information from the collider hit to determine if what you clicked on was the player or not. If it was the player, do what ever you want. If it was anything else, call the public ResetGame method on the script above. That code will look something like this (again, this is not a complete script, I will leave that up to you. This is just to get you going in the right direction):

 //Make sure you have a reference to the PlayerController or whatever you named the script above
 
 private void HandleClick(Collider other) //Or what ever the mouse click script you find calls this function
 {
         if(other.gameObject.CompareTag("Player")) //Make sure  player GameObject is tagged as "Player"
        {
                 //Do whatever you want like adding to your score or adding a force to the player
        }
        else
        {
                 playerController.ResetGame(); //Call ResetGame() on our reference to our first script
                //Do other UI or score saving stuff
         }
 }

I suggest trying to narrow your question down to specific functionality in the future. It can be hard to answer otherwise. But I hope this helps get you going in the right direction! If this helps, please mark it as your answer! Good luck! :)

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

162 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 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 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 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 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 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 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 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 a 2d character three-dimensional? 1 Answer

2D random shooting problem with velocity, 0 Answers

Two Polygon2D Collider do not Collide with Each Other (Solved) 2 Answers

Why does BoxCollider2D distance depend on distance to camera center? 0 Answers

Unity 2D Collider Safe Sizes 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