Я только начал программировать в Unity последние пару месяцев и решил попробовать свои силы в 2D. Я сделал этот контроллер плеера, и он работает хорошо, но когда я тестирую его, прыжок просто не реагирует, независимо от того, что я настраиваю, особенно на контроллере. такое ощущение, что есть полсекунды, когда ты должен был бы подпрыгнуть, но не можешь.
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections;
public class BasicMovement : MonoBehaviour
{
float horizontalInput;
float movespeed = 5f;
float jumpForce = 7f;
public bool isGrounded;
bool wasGrounded;
float dashForce = 15f;
float dashDuration = 0.2f;
int dashCount = 1;
bool isDashing;
bool canDash = true;
float facingDirection = 1f;
Animator anim;
Rigidbody2D rb;
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask groundLayer;
float checkRadius = 0.2f;
void Start()
{
rb = GetComponent();
anim = GetComponent();
}
void OnMove(InputValue value)
{
horizontalInput = value.Get().x;
if (horizontalInput != 0)
{
facingDirection = horizontalInput;
}
}
void OnJump()
{
if (isGrounded)
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
void OnDash()
{
if (canDash && dashCount > 0)
{
StartCoroutine(Dash());
}
}
IEnumerator Dash()
{
isDashing = true;
canDash = false;
dashCount--;
rb.AddForce(new Vector2(facingDirection, 0) * dashForce, ForceMode2D.Impulse);
yield return new WaitForSeconds(dashDuration);
isDashing = false;
}
private void FixedUpdate()
{
if (isGrounded && !wasGrounded)
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, 0f);
}
wasGrounded = isGrounded;
anim.SetFloat("speed", Mathf.Abs(horizontalInput));
anim.SetBool("isGrounded", isGrounded);
if (horizontalInput > 0)//check which way to flip player
{
transform.localScale = new Vector3(3, 3, 3);
}
if (horizontalInput < 0)
{
transform.localScale = new Vector3(-3, 3, 3);
}
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, groundLayer);
// Recharge dash when landing
if (isGrounded && dashCount < 1)
{
dashCount = 1;
canDash = true;
}
if (!isDashing)
{
rb.linearVelocity = new Vector2(horizontalInput * movespeed, rb.linearVelocity.y);
}
}
}