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 pdunton · Jun 06, 2014 at 05:15 AM · javascriptinputmouseupdateif-statements

Input.GetMouseButtonDown(0) running through my if statments too quickly

Hi, so I have this script, and it's so that I can click on the planet icon, move it where I want it, scale the size of the planet, then give it a velocity. Each one of these is its own if statement inside Update(), placingObj, scalingObj, and movingObj, and once you are done with each activity, you click to move to the next one. But, when I click on the planet icon, it sets placingObj to true, and at the end of placingObj it sets scalingObj to true and the same with movingObj, until it goes from clicking on the icon to movingObj all in one frame. I want it so that you have to click individually each time to tell the game you are ready for the next step, and not just so one click rapidly moves through each if statement before the player can do anything. I hope I'm making sense. Thanks!

 #pragma strict
 var objModel : GameObject;
 var mass : int;
 
 var placingObj : boolean = false;
 var scalingObj : boolean = false;
 var movingObj : boolean = false;
 
 var discCreated : boolean = false;
 var disc : GameObject;
 
 var obj : GameObject;
 
 private var rot : Quaternion;
 rot.eulerAngles = new Vector3(270, 0, 0);
 
 var x : int;
 var y : int;
 var pastX : int;
 var pastY : int;
 
 function Start () {
 
 }
 
 function Update () {
 
     if(placingObj){
     
         var pos : Vector3 = Input.mousePosition;
         pos.z = 1;
         pos = Camera.main.ScreenToWorldPoint(pos);
         obj.transform.position = pos;
 
         //Here is where the problem is
         if(Input.GetMouseButtonDown(0)){
             placingObj = false;
             scalingObj = true;
             
         }
     }
     
     if(scalingObj){
         
         if(Input.GetAxis("Mouse ScrollWheel") > 0  && mass >= 5){
             obj.rigidbody.mass += 5;
             mass -= 5;
         }
         if(Input.GetAxis("Mouse ScrollWheel") < 0 && obj.rigidbody.mass >= 10){
             obj.rigidbody.mass -= 5;
             mass += 5;
         }
         obj.rigidbody.mass = Mathf.RoundToInt(obj.rigidbody.mass);
         
         //Here is where the problem is
         if(Input.GetMouseButtonDown (0)){
             mass = Mathf.RoundToInt(mass - obj.rigidbody.mass);
             scalingObj = false;
             movingObj = true;
         }
     }
     
     if(movingObj){
         //Still yet to do
     }
     
     //This is just a canceling feature
     if(Input.GetMouseButton(1)){
          mass += obj.rigidbody.mass;
          GameObject.Destroy(obj);
          placingObj = false;
          scalingObj = false;
          movingObj = false;
     }
     
     if(mass == 499){
         mass = 500;
     }
     gameObject.guiText.text = Mathf.RoundToInt(mass).ToString();
     
 }
 
 function OnMouseDown(){
     if(!placingObj && !scalingObj && !movingObj){
         obj = Instantiate(objModel, Vector3.zero, rot);
         placingObj = true;
         mass -= obj.rigidbody.mass;
     }
 }
 
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 oasisunknown · Jun 06, 2014 at 06:10 AM

I would suggest rearranging the order that you write your code because currently when you push the mouse button all it does is run the bool switches. I would switch your code to call a function to do what you want and then in the update just call the function. below is how I would rearrange the first function.

     function PlaceObj ()
     {
     //note that I moved the input check into the if statement.
       if(placingObj && Input.GetMouseButtonDown(0))
       {
      //run the movement code if the moose is down.
        var pos : Vector3 = Input.mousePosition;
        pos.z = 1;
        pos = Camera.main.ScreenToWorldPoint(pos);
        obj.transform.position = pos;
       }
     //after the mouse is no longer down then run the bool switch.
     placingObj = false;
     scalingObj = true;
     //end function 
     }
 


I cant guarantee that this will fix your issues but its something to try in the mean time.

also note you don't have to put the code into a function I just thought it would help readability and it ensures that the bool switch is the very last thing to happen as the function exits.

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

22 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

Related Questions

New Input system holding down a button 2 Answers

encapsulating if statement in update 1 Answer

Input Axis Mouse ScrollWheel 1 Answer

Checking if statement in higher frequency 0 Answers

Eliminating input loss 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