キャラクターが攻撃されるたびに「ノックバック」を追加し、ゲームの操作に変更を加え、キーボードのキーの使用から「電話」のキーの使用に切り替えましたが、なぜ私のキャラクターが理解できない場合私はじっと立っていて、彼は敵からのノックバックを受けず、ただアニメーションを実行しますが、私が動くと、彼はもう押し戻されず (ノックバック)、アニメーションを実行している場合でも、彼は正常に移動できます。
私がじっとしていると、彼は押し戻され、すべてがうまく機能します。「PC移動」を左右に移動する3つのUIボタンに置き換えたため、別のRigidBody(敵)と衝突したときに左右に移動し、ジャンプする3つのボタンを無効にすることが解決策になるのではないかと考えました。そしてジャンプする人。
これが私の「フルモーションプレーヤー」コードです:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
[SerializeField] private Animator anim;
private enum State { idle, running, jumping, falling, hurt };
private State state = State.idle;
private Collider2D coll;
[SerializeField] private LayerMask ground;
[SerializeField] private float speed = 5f;
[SerializeField] private float jumpForce = 8f;
[SerializeField] private float hurtForce = 5f;
[SerializeField] public int cherries = 0;
[SerializeField] private TextMeshProUGUI cherryText;
[SerializeField] private AudioSource cherry;
[SerializeField] private AudioSource footsteps;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<Collider2D>();
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Nemico")
{
Enemy enemy = other.gameObject.GetComponent<Enemy>();
if (state == State.falling)
{
Vector2 speed2 = rb.velocity;
speed2 = jumpForce * Vector2.up;
rb.velocity = speed2;
Jump();
enemy.JumpedOn();
}
else
{
state = State.hurt;
if (other.gameObject.transform.position.x > transform.position.x)
{
// il Nemico è sulla mia destra e se vengo colpito mi sposto a sinistra
rb.velocity = new Vector2(-hurtForce, rb.velocity.y);
}
else
{
// il Nemico è sulla mia sinistra e se vengo colpito mi sposto a destra
rb.velocity = new Vector2(hurtForce, rb.velocity.y);
}
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Collezionabili")
{
cherry.Play();
Feedback collect = collision.gameObject.GetComponent<Feedback>();
collect.Collect();
cherries += 1;
cherryText.text = cherries.ToString();
}
}
private void Update()
{
Movement();
if (state != State.hurt)
{
Movement();
}
AnimationState();
anim.SetInteger("state", (int)state);
if (CrossPlatformInputManager.GetButtonDown("Jump"))
{
Jump();
}
else
{
}
}
private void Movement()
{
float hDirection = Input.GetAxis("Horizontal");
hDirection = CrossPlatformInputManager.GetAxis("Horizontal");
if (hDirection < 0)
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
transform.localScale = new Vector2(-1, 1);
}
else if (hDirection > 0)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
transform.localScale = new Vector2(1, 1);
}
}
public void Jump()
{
if (coll.IsTouchingLayers(ground) == true)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
state = State.jumping;
}
}
private void AnimationState()
{
if (state == State.jumping)
{
if (rb.velocity.y < .1f)
{
state = State.falling;
}
}
else if (state == State.falling)
{
if (coll.IsTouchingLayers(ground))
{
state = State.idle;
}
}
else if (state == State.hurt)
{
if (Mathf.Abs(rb.velocity.x) < .1f)
{
state = State.idle;
}
}
else if (Mathf.Abs(rb.velocity.x) > 2f)
{
state = State.running;
}
else
{
state = State.idle;
}
}
}