玩家没有受到尖刺的伤害

时间:2019-02-03 18:46:07

标签: c# unity3d

我正在开发一款平台游戏,我为Health创建了UI,然后又为Player建立了Health。但是当我创建Sprike脚本时,它没有用。例如,当我与尖峰交叉时(与Box colider,是触发),它将重置级别。

我还创建了一个void Dead,如果curHealth <= 0则重置级别,但是我不知道为什么如果我还有2条生命(玩家有3条公共生命int maxHealth = 3;,它会重新启动我的lvl )

最烂的是:

穗脚本:

using System.Collections;
using System.Collections.Generic;

using UnityEngine;

public class Spikes : MonoBehaviour

{

    private Player player;

    void Start(){

        player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
    }

    void OnTriggerEnter2D(Collider2D col){

        if(col.CompareTag("Player")) {

            player.Damage(1);
        }
    }
}

播放器脚本:

using System.Collections;

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
    public GameObject blood;

    public float speed;
    public float jumpForce;
    private float moveInput;

    private Rigidbody2D rb;


    private bool isGrounded;
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask whatIsGround;

    private int extraJumps;
    public int extraJumpsValue;

    private bool facingRight =  true;

    public int curHealth;
    public int maxHealth = 3;

    void Start(){

        curHealth = maxHealth;

        extraJumps = extraJumpsValue; 
        rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate(){

        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

        moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

        if(facingRight == false && moveInput > 0){
            Flip();
        } else if(facingRight == true && moveInput < 0){
            Flip();
        }
    }

    void Update(){

        if(isGrounded == true){
            extraJumps = extraJumpsValue;
        }

        if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0){
            rb.velocity = Vector2.up * jumpForce;
            extraJumps--;
        } else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true){
            rb.velocity = Vector2.up * jumpForce;
        }

        if(curHealth > maxHealth){
            curHealth = maxHealth;
        }

        if(curHealth <= 0){
            Die ();
        }


       }
    void Flip(){

        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }

    void Die(){

        //Restart
        Application.LoadLevel(Application.loadedLevel);
    }

    void OnCollisonEnter2D(Collision2D col){
        if (col.gameObject.tag.Equals("Spikes")){

            Instantiate ( blood, transform.position, Quaternion.identity);
            gameObject.SetActive(false);
        }
    }

    public void Damage(int dmg) {

        curHealth -= dmg;
    }
}

2 个答案:

答案 0 :(得分:0)

您的代码检查gameObject / collider的标签。如果有3个对撞机,则每一个对撞机都将调用Player.Damage()。 如果您想坚持自己的设计,则需要将它们拆分为子对象,并为它们提供不同的标签,例如“ PlayerParent”和“ PlayerChild”或类似的标签。

您还可以对父播放器对象的每个子对象使用相同的标签,但在与父对象不同的层上使用子标签。查看如何忽略图层:https://docs.unity3d.com/ScriptReference/Physics.IgnoreLayerCollision.html

只需尝试从OnTriggerEnter2D函数中检测到其他不重要的对撞机即可。也许您可以切换到OnCollisionEnter2D并且不让播放器“掉进去”? 希望这能回答您的问题。 干杯。

答案 1 :(得分:0)

OK在刚体上有一个称为Collision Detection的属性,将其更改为离散(如果是连续的),然后重试