I have a problem with GUI.RepeatButton that I’m hoping someone might be able to help with.
My main_interface script has a few blocks for controlling the player’s movement. For example, this for moving forward:
if (showGUI)
{
if (GUI.RepeatButton(Rect(370, 210, 50, 50), texGoForward, persistence_ref.styleNormalWhite))
{
if (!isMoving !isShaking canMoveForward)
GoForward();
if (!isMoving !isShaking !canMoveForward)
ShakeCamera();
}
}
This allows the player to hold down the “Forward” button and move forward constantly.
The problem is, I have these teleporters that the player can walk into, which repositions them on the level. Those have a separate script attached that contains this:
function OnTriggerEnter(obstacle : Collider)
{
if (!persistence_ref.eventInProgress)
{
main_interface_ref.showGUI = false;
persistence_ref.eventInProgress = true;
sprite_auto_rotation_ref.enabled = false;
audio.PlayOneShot(persistence_ref.audioTeleport);
yield WaitForSeconds(persistence_ref.audioTeleport.length);
player_ref.transform.position = destination_ref.transform.position;
yield WaitForSeconds(1.0);
main_interface_ref.UpdateMovement();
sprite_auto_rotation_ref.enabled = true;
persistence_ref.eventInProgress = false;
main_interface_ref.showGUI = true;
}
}
So basically, they hit the teleporter, it hides the main interface (which includes the GUI.Repeatbutton that’s moving them forward), they get transported to the new location, and the main interface reappears.
The issue is, regardless of whether or not I’m is still holding my finger down (i.e. I might not be touching the screen at all), when I regain the interface after teleporting, the player object will keep moving forward indefinitely like I’m still holding down the button, even though in between I’ve stopped even showing the GUI.
I can’t figure out any way to stop it happening. Anyone have ideas?
For reference, this is the GoForward() function that’s being called by the GUI.RepeatButton:
function GoForward()
{
isMoving = true;
PlayFootStepAudio();
startPosition = transform.position;
if (facing == 0)
endPosition = transform.position - Vector3(0.0, 0.0, -gridSize);
if (facing == 1)
endPosition = transform.position - Vector3(-gridSize, 0.0, 0.0);
if (facing == 2)
endPosition = transform.position - Vector3(0.0, 0.0, gridSize);
if (facing == 3)
endPosition = transform.position - Vector3(gridSize, 0.0, 0.0);
t = 0.0;
while (t < 1.0)
{
t += Time.deltaTime * (walkSpeed/gridSize);
transform.position = Vector3.Lerp(startPosition, endPosition, t);
yield;
}
isMoving = false;
UpdateEndurance();
UpdateMovement();
}