Answer the question
In order to leave comments, you need to log in
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
Rigidbody rb;
bool jumpedTwice; // Значение true, когда персонаж прыгнул второй раз.
bool isPlayerOnGround; // Значение true, когда персонаж стоит на земле.
public void Jump()
{
if(!jumpedTwice)
{
if(!isPlayerOnGround)
{
isJumpedTwice = true;
}
rb.AddForce(transform.up * 5, ForceMode.Impulse);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question