Q
Q
qwertygameover2021-03-12 16:19:14
Unity
qwertygameover, 2021-03-12 16:19:14

What is wrong with the jump implementation?

So, I'm a macro, I could not find a ready-made button in the assets store, and decided to improvise, I created a button on the screen, and gave it the name "Jump"

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody _rigidbody;
    public FixedJoystick joystick;
    public Transform groundCheck;
    public LayerMask groundMask;

    public float playerspeedRL = 5f;
    private float gravity = -9.81f;
    public float radius = 0.72f;

    bool isGrounded;

    Vector3 velocity;

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

    void Update()
    {

    }

    void FixedUpdate()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, radius, groundMask);

        if (isGrounded)
        {
            _rigidbody.velocity = transform.forward * 5 + transform.right * joystick.Direction.x * playerspeedRL;
        }
        else
        {
            _rigidbody.velocity = transform.forward * 5 + transform.right * joystick.Direction.x * playerspeedRL + transform.up * gravity;
        }



        //_rigidbody.velocity = transform.forward * 5 + transform.right * joystick.Direction.x * playerspeedRL + transform.up * gravity;

    }

    public void JumpScript()
    {
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            _rigidbody.velocity = transform.up * 30;
            Debug.Log("Jump");
        }
    }

}


And I first tried to push this piece of undercode into Update, then I created a separate function for it and tied it to the button, but nothing works, which can be done so that by clicking on the created UI.Button, this code occurs.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2021-03-12
@freeExec

In the inspector, the button has a OnClickdrag-and-drop component on which you will call the method, and then you will select it there.

G
GavriKos, 2021-03-12
@GavriKos

Input.GetButtonDown("Jump")

has nothing to do with the button on the screen. Open the manual and read what this method does

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question