- Home /
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;
}
}
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.
Your answer
Follow this Question
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