每次被击中时我都会添加角色的“击退”,现在我对游戏控件进行了一些更改,并从使用键盘键切换到使用“电话”键,但我不明白为什么我的角色如果我站着不动,他不会受到敌人击中他的击退,他只是做动画,但如果我移动,他不会再被推回(击退),即使他仍在做动画,他也可以正常移动。
如果我保持不动,他就会被推回去,一切正常。我认为解决方案可能是:禁用使我在与另一个 RigidBody(敌人)碰撞时向左、向右和跳跃的三个按钮,因为我用三个 UI 按钮向左和向右移动替换了“PC 移动”,还有一个跳。
这是我的“全动作播放器”代码:
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;
}
}
}