- Home /
Question by
utAlpha09 · Nov 27, 2013 at 06:37 PM ·
guicharactercontrollerjumpguibutton
Im trying to make a character jump when a GUI button is clicked on
Hello, I am trying to make a player with a character controller jump when a button is clicked onscreen however the character will not jump. Here is my code so far var speed : float = 3.0; var rotateSpeed : float = 3.0; var moveJoystick : Joystick; var rotateJoystick : Joystick; var jumpSpeed : int =5; var gravity : int = 32; private var moveDirection : Vector3 = Vector3.zero; private var grounded = false;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
var rotatePos = Input.GetAxis ("Horizontal") ?
Input.GetAxis ("Horizontal") : joyStickInput(rotateJoystick);
transform.Rotate(0, rotatePos * rotateSpeed, 0);
// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var movePos = Input.GetAxis ("Vertical") ?
Input.GetAxis ("Vertical") : joyStickInput(moveJoystick);
var curSpeed = speed * movePos;
controller.SimpleMove(forward * curSpeed);
}
function joyStickInput (joystick : Joystick) {
var absJoyPos = Vector2 (Mathf.Abs(joystick.position.x),
Mathf.Abs(joystick.position.y));
var xDirection = (joystick.position.x > 0) ? 1 : -1;
var yDirection = (joystick.position.y > 0) ? 1 : -1;
return ( ( absJoyPos.x > absJoyPos.y) ? absJoyPos.x * xDirection : absJoyPos.y * yDirection);
}
function OnCollisionStay ()
{
grounded = true;
}
function OnGUI(){
if(GUI.Button(Rect(Screen.width /2.7 + 0,Screen.height /1 - 100,500,100), "Jump")){
}
}
@script RequireComponent(CharacterController)
I cannot seem to make the character jump by adding code to function OnGUI(){}.
Comment
Answer by jacksmash2012 · Nov 27, 2013 at 06:53 PM
You OnGUI code is listening for a button press, but it does absolutely nothing when the button is actually pressed!
You will need something like this:
function OnGUI(){
if(GUI.Button(Rect(Screen.width /2.7 + 0,Screen.height /1 - 100,500,100), "Jump")){
Jump();
}
}
function Jump(){
// jump code here...
}