微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

玩家被击中时的“击退”

如何解决玩家被击中时的“击退”

我每次被击中时都添加了我的角色的“击退”,现在我对游戏控制进行了一些更改,我从使用键盘键切换到“电话”键,但我不明白为什么我的角色如果我站着不动,他不会被敌人击退 他只是做动画,但如果我移动,他就不会再被推回(击退),即使他还在做动画,他也可以正常移动。

如果我保持不动,他就会被推后,一切正常。我认为解决方案可能是: 当我与另一个刚体(敌人)发生碰撞时,禁用让我向左、向右和跳跃的三个按钮,因为我用三个 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;
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?