Версия Unity 2019.3.6f
Например, я хочу сделать так, что при зажатии клавиши Space, каждый раз касаясь земли он прыгал снова.
Вот скрипт всех движений игрока:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Control : MonoBehaviour
{
public float speed = 20f;
private Rigidbody2D rb;
private bool FaceRight = true;
public float JumpForse;
private bool is_grounded;
public Transform ground_check;
public float check_radius;
public LayerMask what_is_ground;
private int extra_jumps;
public int extra_jump_value;
private float move_input;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
extra_jumps = extra_jump_value;
}
private void Update()
{
if(is_grounded == true)
{
extra_jumps = extra_jump_value;
}
if (Input.GetKeyDown(KeyCode.Space) && extra_jumps > 0)
{
rb.velocity = Vector2.up * JumpForse;
extra_jumps--;
}
else if (Input.GetKeyDown(KeyCode.Space) && extra_jumps == 0 && is_grounded == true)
{
rb.velocity = Vector2.up * JumpForse;
}
}
void flip()
{
FaceRight = !FaceRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
private void FixedUpdate()
{
is_grounded = Physics2D.OverlapCircle(ground_check.position, check_radius, what_is_ground);
if (FaceRight == false && move_input > 0)
{
flip();
} else if(FaceRight == true && move_input < 0)
{
flip();
}
move_input = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(move_input * speed, rb.velocity.y);
}
}