Q
Q
qwertygameover2021-03-12 18:13:01
Unity
qwertygameover, 2021-03-12 18:13:01

How to make a 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 = new Vector3(5 * joystick.Direction.x, 0, 5);
            //_rigidbody.velocity = transform.forward * 5 + transform.right * joystick.Direction.x * playerspeedRL;
        }
        else
        {
            _rigidbody.velocity = new Vector3(5 * joystick.Direction.x, gravity, 5);
            //_rigidbody.velocity = transform.forward * 5 + transform.right * joystick.Direction.x * playerspeedRL + transform.up * gravity;
        }


    }


    public void JumpScript()
    {
        if (isGrounded)
        {
            _rigidbody.velocity = new Vector3(0, 30, 0);
            //_rigidbody.velocity = transform.up * 30;
            Debug.Log("Jump");
        }
    }

}


And so it’s been +- an hour and a half trying to sort out this problem, I have 2 questions, there are 2 pieces of code, 1 is outlined, it is made without new Vector3, the second is not outlined and it is already done through new Vector3, 1 question, how does new Vector3 differ from just velocity changes, and why isn't this box jumping? Specifically, pressing the button passes, but it also continues to crawl forward, I understand exactly why this is happening, the function with movement blocks the jump because it is in Update, but I don’t understand how to fix it, I tried to stop the code for a while when jumping, but I guess I'm disabled.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question