[SOLVED]Gui button onRelease

hey folks,

i got this little problem with some buttons of mine.
What i want is this:
when i push a button on my screen it has to send a message to another object->script that tells him to walk forward
but it sends the message when you release the button.
it has to be when you hold the button down that it has to send the message.

this is the short script (the whole script is like 500 lines)

var camera1object : GameObject;
var camera2object : GameObject;

var ArrowSize = Vector2(20,20);
var upImage : Texture2D;


function OnGUI ()
{
	if (GUI.Button(Rect(0,0,ArrowSize.x,ArrowSize.y),upImage))InputForward();
}

function InputForward()
{
	if (camera1object.active)
	{
		camera1object.SendMessage ("walkForward", 1);
	}
	
	if (camera2object.active)
	{
		camera2object.SendMessage ("walkForward", 1);
	}
}

This script is the standard FPSWalker with some addons.

var speed = 0.0;
var loopspeed = 6.0;
var sprintspeed = 9.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var turnSpeed = 0.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function Update ()
{
	print(speed);
}

function FixedUpdate() {
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		speed = loopspeed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
			}
		if (Input.GetButton ("Sprint")) {
			speed = sprintspeed;
			
		}
	}

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
	
	   // Check to see if the right mouse button is pressed
   if (Input.GetMouseButton(1))
   {
      // Get the difference in horizontal mouse movement
      x = Input.GetAxis("Mouse X") * turnSpeed;
   }

   // rotate the character based on the x value
   transform.Rotate(0, x, 0);
   //print (transform.rotation.eulerAngles);
}

function walkForward(val:float)
{
	//transform.Rotate(0, 1 * turnSpeed, 0);
	Debug.Log("My finger is on the button");
}

if you need some more info ask me.
Thnx!

There is another type of control called a RepeatButton that is active while it is held down. It is otherwise similar to the Button control, so I think this should do what you want.

Yup thnx! that works!

so i want my camera to move
now i changed this piece of code

function walkForward(val:float)
{
   //transform.Rotate(0, 1 * turnSpeed, 0);
   Debug.Log("My finger is on the button");
}

for this piece of code

function walkForward(val:float)
{
   moveDirection.z = 3.0;
   Debug.Log("My finger is on the button");
}

i made a test button for when i push the “T” it walks
and i use this “moveDirection.z = 3.0;” when i push the “T”… that works… but not with that button.
i hope i makes any sense…

Thnx!

bump

You have this line:-

 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

This happens at the start of the frame update. Any value set for moveDirection.z will be replaced every frame by the input value. You either need to remove this line or do something like:-

var zVal = Mathf.Max(moveDirection.z, Input.GetAxis("Vertical"));
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, zVal);

well that’s what i thought… but i’ll get shot out of a cannon like that…
normal speed in a second is 5 meters.
after i put this thing in the script it’s like a 100 meters in a second

The most obvious explanation for the extra speed is that Input.GetAxis returns values between 0 and 1 (or -1). Since you’re setting the speed to 3, it would be much faster than normal.

yup you are right!
that was another stupid mistake from me. :sweat_smile:

var buttonwalk : int = 0;

var zVal = Mathf.Max(buttonwalk, Input.GetAxis("Vertical"));
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, zVal);

function walkForward(val:float)
{
   buttonwalk = 1;
   Debug.Log("My finger is on the button");
}

that works… only thing i have to do is when i release the button that it has to stop walking.
but that ain’t a problem.
Thank you verry much!