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 Bogdan26 · Dec 27, 2017 at 07:00 PM · if-statementsmousepositioncoroutines

Trying to make a game that tests whether the player has pressed on the left or on the right side of the screen but it doesn't work

The game is supposed to be a very unoriginal coordination game, where there's an arrow and a text displayed on the screen (both of them either display left or right randomly) and you have to click on the side that the text is showing.

 IEnumerator Game() {
     timeLeft = 15f;
     int randomNumberArrow = Random.Range (-1, 2); // Determines whether the ARROW shows left or right
     int randomNumberText = Random.Range (-1, 2); // Determines whether the TEXT shows left or right

     if (randomNumberArrow == 0) { // ARROW shows LEFT
         arrow.transform.rotation = Quaternion.Euler(0f, 0f, 180f);
     } else { // ARROW shows RIGHT
         arrow.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
     }

     if (randomNumberText == 0) { // TEXT shows LEFT
         leftOrRight.text = "LEFT";
         if (Input.GetMouseButtonDown(0))  { // Tests if the player taps the correct(left) side
             if (Input.mousePosition.x < 240) {
                 pressedCorrectly = true;
             } else {
                 pressedCorrectly = false;
             }
         }
     } else { // TEXT shows RIGHT
         leftOrRight.text = "RIGHT";
         if (Input.GetMouseButtonDown(0))  { // Tests if the player taps the correct(right) side
             if (Input.mousePosition.x > 240) {
                 pressedCorrectly = true;
             } else {
                 pressedCorrectly = false;
             }
         }
     }
     if (pressedCorrectly) { // Player Wins
         StartCoroutine ("Game", 0.1f);
         score++;
         Debug.Log (score);
     } else { // Player Fails by clicking incorrectly
         GameOver ();
         StopCoroutine ("Game");
     }

     yield return new WaitForSeconds (timeLeft);
     if ( timeLeft <= 0 ) // Player Fails by not clicking in time
     {
         GameOver();
         StopCoroutine ("Game");
     }
 }

When I hit play I instantly get the Game Over screen. I believe this has something to do with the if statements (where the "TEXT shows RIGHT" and "TEXT shows LEFT" comments are). Note that 240 is the x position that's in the center of the screen.

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
Best Answer

Answer by Taylor-Libonati · Dec 27, 2017 at 09:12 PM

Is this a school project? It looks like you are trying to create your own game loop but that's not really how Unity works. You should be creating a monobehaviour script that attaches to a game object and then just handle your game logic in the Update loop.

But that aside, here is your problem. You are checking your random numbers and checking the input then at line 31 you check if they pressed the right button but its now only been 1 frame in the game. So of course no one can hit the button in 1 frame so it goes to a game over. If they did somehow have the mouse down when you hit play it would start another game loop on top of the one you are in, which is also not what you want.

I would suggest doing a couple unity scripting tutorials to figure out how to properly use the Update loop. But to make what you have work you would have to not do anything (keep looping) until a button is pressed. So like have a bool that is buttonPressed and it is only true if anything has been pressed. If it is true then you check pressedCorrectly. If it is false you just continue the loop.

Something like this:

 while(!buttonPressed){
      //All your input stuff
 }
 if(pressedCorrectly){
      //The rest of the stuff



Comment
Add comment · Show 2 · 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 Bogdan26 · Dec 28, 2017 at 07:08 AM 0
Share

Not a school project, more like a challenge for myself. Thanks! I'll check out the scripting tutorials right now.

avatar image Taylor-Libonati Bogdan26 · Dec 28, 2017 at 05:31 PM 0
Share

Cool :) Good luck!

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

72 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

Related Questions

If Loop For Key Press works but not for Mouse 0 Answers

Why simple letter evaluataion returns true no matter witch letter 1 Answer

How to resize a player every couple of seconds? 2 Answers

Problem when checking a Quaternion Eulerangle axis 1 Answer

[SOLVED]Different levels of damage per particle collision 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