Я пытаюсь сделать свою первую игру 2D-платформером, но столкнулся с проблемой, которую не могу понять. Итак, у меня есть персонаж на полу, и когда я пытаюсь прыгнуть, иногда он прыгает и воспроизводит анимацию прыжка, иногда просто прыгает. У меня есть обе инструкции (о скорости и анимации) в одном if, поэтому я не вижу, что там происходит. Я оставлю код ниже, если кто-то может помочь. Я также открыт для звонков в раздоре или по скайпу. Заранее спасибо!
Скрипт плеера:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.XR;
public class Player : MonoBehaviour
{
private Rigidbody2D rb;
private PlayerAnimations playerAnim;
private SpriteRenderer playerSprite;
//JUMPING
[SerializeField]
private float jumpingForce = 4.5f;
[SerializeField]
private float movementSpeed = 3;
private float fallMultiplier = 2f;
private bool isGrounded = false;
void Start()
{
rb = GetComponent<Rigidbody2D>();
playerAnim = GetComponent<PlayerAnimations>();
playerSprite = GetComponentInChildren<SpriteRenderer>();
}
void Update()
{
Movement();
MeeleAttack();
}
void Movement()
{
//MOVING
float horizontalInput = Input.GetAxisRaw("Horizontal");
isGrounded = IsGrounded();
Flip(horizontalInput);
rb.velocity = new Vector2(horizontalInput * movementSpeed, rb.velocity.y);
playerAnim.Move(horizontalInput);
//JUMPING
SmootherJump();
}
bool IsGrounded()
{
RaycastHit2D isGrounded = Physics2D.Raycast(transform.position, Vector2.down, 0.3f, 1 << 8);
Debug.DrawRay(transform.position, Vector2.down * 0.3f, Color.green);
if (isGrounded.collider != null)
{
playerAnim.Jump(false);
return true;
}
return false;
}
void SmootherJump()
{
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.W) && IsGrounded() == true)
{
rb.velocity = new Vector2(rb.velocity.x, jumpingForce);
playerAnim.Jump(true);
}
}
void Flip(float horizontalInput)
{
if (horizontalInput > 0)
{
playerSprite.flipX = false;
}
else if (horizontalInput < 0)
{
playerSprite.flipX = true;
}
}
void MeeleAttack()
{
if (Input.GetMouseButtonDown(0) && IsGrounded() == true)
{
playerAnim.MeeleAttack();
}
}
}
Скрипт анимации плеера:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAnimations : MonoBehaviour
{
private Animator anim;
void Start()
{
anim = GetComponentInChildren<Animator>();
}
public void Move(float horizontalInput)
{
anim.SetFloat("Move", Mathf.Abs(horizontalInput));
}
public void Jump (bool jumping)
{
anim.SetBool("Jumping", jumping);
}
public void MeeleAttack()
{
anim.SetTrigger("Meele Attack");
}
}