c# - 玩家没有受到尖刺的伤害
问题描述
我正在开发一个平台游戏,我为 Health 创建了 UI,然后我创建了 Player health。但是当我创建 Sprike 脚本时,它没有工作。就像,当我与尖峰交互时(使用 Box colider,是触发器)它重置级别。
我还创建了一个void Dead
,如果 ,则重置级别curHealth <= 0
,但我不知道为什么如果我还剩 2 条生命它会重新启动我的 lvl(玩家有 3 条公共生命int maxHealth = 3;
)
脚本是:
尖峰脚本:
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;
}
}
解决方案
好的在刚体上有一个名为碰撞检测的属性将其更改为离散的(如果它是连续的)并重试
推荐阅读
- amazon-cognito - CDK 在创建的用户池中创建新用户
- firebase - Nuxt 中间件中的重定向功能使状态为空
- python - 如何求解具有多个等于的 Sympy 方程?
- azure - Azure 安全中心-VM 更新-建议/修复
- python - spark.read.options(header=True, delimiter="|").csv("mycsv") PySpark 中的 3 行花费了太多时间
- mysql - 如何为特定值添加计数列(作为子选择)
- rx-swift - 如何在主线程中执行 do 运算符的 onSubscribe 操作
- apache-flink - flink管道中的事件丢失
- c# - 如何在 C# 中引用精灵的宽度?
- flutter - 如何在行中的容器下方写入文本