Can I use UI buttons to move my character? I’ve always used WASD or the arrows to steer my character, but now I want to get my app into my device so I have to use the UI buttons. Is this possible?
This is my script I’m using with WASD and the arrows. I want to convert this script to UI buttons instead of WASD and arrows.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var turnRate : float = 2;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
var turnForce = Input.GetAxis("Horizontal") * turnRate;
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3 (0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
transform.Rotate(0,turnForce,0);
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}