K
K
Kelni2020-08-01 18:24:40
Unity
Kelni, 2020-08-01 18:24:40

How to double jump in unity3d for android?

how to do a double jump? otherwise he jumps endlessly (please explain in an accessible way because I have only been in the unit for a couple of months)

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

public class Player : MonoBehaviour
{
    private float moveInput;
    private bool facingRight = true;
   
    public FixedTouchField touchField;
    public Joystick joystick;
    

    Rigidbody rb;
    float xMov;
    float zMov;

    float yRot;
    public int speed = 5;


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

    

    private void FixedUpdate()
    {

        xMov = joystick.Horizontal();
        zMov = joystick.Vertical();

        yRot = touchField.TouchDist.x / 20;

        Vector3 MovHor = transform.right * xMov;
        Vector3 MovVer = transform.forward * zMov;

        Vector3 velocity = (MovHor + MovVer).normalized * speed;
        Vector3 rotation = new Vector3(0, yRot, 0) * speed;

        rb.MovePosition(rb.position + velocity * Time.deltaTime);
        rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
         


    }


    public void Jump()
    {
        rb.AddForce(transform.up * 5, ForceMode.Impulse);
    }




    




}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2020-08-02
@Kelni

Rigidbody rb;
    bool jumpedTwice; // Значение true, когда персонаж прыгнул второй раз.
    bool isPlayerOnGround; // Значение true, когда персонаж стоит на земле.
    public void Jump()
    {
        if(!jumpedTwice)
        {
            if(!isPlayerOnGround)
            {
                isJumpedTwice = true;
            }
            rb.AddForce(transform.up * 5, ForceMode.Impulse);
        }
    }

However, you need to figure out how you will do the isPlayerGrounded check, there are (I know) two options. One of them is checking for a collision with a collider belonging to an object with a specific tag.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question