- Home /
 
UI buttons to move character?
Can I use UI buttons to move my character? I'm supposed to click a UI button like an arrow or something and my character should slide forward. (My character is a cube btw) It is supposed to slide along the floor and may turn and slide backwards too. I have a script that causes my character to move forward and backward on turn left and right, but the problem is that I can only use my arrows or WASD on my keyboard to move. I want to change those keys with UI buttons that I can touch them on my device to move the character. Is that possible?
This is what I got:
 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);
 }
 
              
               Comment
              
 
               
              Your answer