0
\$\begingroup\$

There is a jump script and it works correctly on my pc. How can I use UI button instead of Space bar?

there is three input in my code

Input.GetKeyDown(KeyCode.Space)

Input.GetKey(KeyCode.Space)

Input.GetKeyUp(KeyCode.Space)

and I just want to replace Space bar to UI button, please somebody help me <3.

    private Rigidbody rb; 
    public float jumpForce;        
    private bool isGrounded;        
    public Transform feetPos;       
    public float circleRadius;      
    public LayerMask whatIsGround;  
    public float jumpTime;          
    private float jumpTimeCounter; 
    private bool isJumping;       

    void Start ()
    {
        rb = GetComponent<Rigidbody>();  
    }

    private void Update()
    {
        isGrounded = Physics.CheckSphere(feetPos.position, circleRadius, whatIsGround);

        if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))  
        {
            isJumping = true;                    
            jumpTimeCounter = jumpTime;                
            rb.velocity = Vector2.up * jumpForce;       
        }

        if (Input.GetKey(KeyCode.Space) && isJumping == true)
        {
            if (jumpTimeCounter > 0)                    
            {
                rb.velocity = Vector2.up * jumpForce;   
                jumpTimeCounter -= Time.deltaTime;      
            }
            else                                        
            {
                isJumping = false;                     
            }
        }

        if (Input.GetKeyUp(KeyCode.Space))              
        {
           isJumping = false;                          
        }
    }

\$\endgroup\$
1
  • \$\begingroup\$ What have you tried? Where are you stuck? Creating an UI button? Reading input from it? Something else? \$\endgroup\$
    – Tyyppi_77
    Jun 26 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.