I want to make movement using on-screen buttons that you can hold down, unfortunately while the keyboard inputs work as intended, the on-screen inputs just won’t. They move the controlled object way too fast for some reason when I try to use those “else” statements in “On GUI” and if I try to use booleans to control it, the same thing happens. If I remove or comment out the else statements in OnGUI I get normal movement, albeit not what I intend (I want it to snap to integers after the controls are let go of). So… any ideas as to why this is happening and how to fix it?
#pragma strict
var speed : float = 3;
var int_position : int;
var negative_int_position : int;
var move_left : boolean;
var move_right : boolean;
var on_screen_only : boolean;
var keyboard : boolean;
var keys_or_screen : String;
function Start () {
}
function Update () {
if(keyboard == true)
{
if(Input.GetKey(KeyCode.D))
{
transform.position.x += speed * Time.deltaTime;
}
else if(Input.GetKeyUp(KeyCode.D))
{
int_position = Mathf.CeilToInt(transform.position.x);
if(transform.position.x != int_position)// && transform.position.x > 0)
{
transform.position.x = int_position;
}
}
if(Input.GetKey(KeyCode.A))
{
transform.position.x -= speed * Time.deltaTime;
}
else if(Input.GetKeyUp(KeyCode.A))
{
negative_int_position = Mathf.FloorToInt(transform.position.x);
if(transform.position.x != negative_int_position)
{
transform.position.x = negative_int_position;
}
}
}
}
function OnGUI () {
if(GUI.Button(new Rect(0,50,100,50),"On-Screen"))
{
on_screen_only = true;
keyboard = false;
keys_or_screen = "On-Screen";
}
if(GUI.Button(new Rect(100,50,100,50),"Keyboard"))
{
on_screen_only = false;
keyboard = true;
keys_or_screen = "Keyboard";
}
GUI.Box(new Rect(200,50,100,50),keys_or_screen);
if(on_screen_only == true)
{
if(GUI.RepeatButton(new Rect(0,Screen.height - 50,100,50),"<-"))
{
//move_left = true;
transform.position.x -= speed * Time.deltaTime;
}
else
{
//move_left = false;
negative_int_position = Mathf.FloorToInt(transform.position.x);
if(transform.position.x != negative_int_position)
{
transform.position.x = negative_int_position;
}
}
if(GUI.RepeatButton(new Rect(100,Screen.height - 50,100,50),"->"))
{
transform.position.x += speed * Time.deltaTime;
}
/*else
{
int_position = Mathf.CeilToInt(transform.position.x);
if(transform.position.x != int_position)// && transform.position.x > 0)
{
transform.position.x = int_position;
}
}*/
}
}