c# - 如何因与敌人碰撞而损失伤害
问题描述
嗨,我对统一和 C# 非常陌生,因此非常感谢任何帮助!我试图让我的敌人(预制件名为雪人)和我的玩家(预制件名为圣诞老人)发生碰撞。发生这种情况时,玩家应该在其生命条中失去伤害。健康条出现了,但我对如何让它在与雪人碰撞时失去健康感到迷茫。
这是脚本:
using UnityEngine;
using System.Collections;
using Mirror;
using UnityEngine.UI;
public class Player : NetworkBehaviour {
CharacterController characterController;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public int maxHealth = 30;
public int currentHealth;
//private HealthBar healthBar;
private Vector3 moveDirection = Vector3.zero;
void TakeDamage(int damage) {
currentHealth -= damage;
FindObjectOfType<HealthBar>().SetHealth(currentHealth);
// healthBar.SetHealth(currentHealth);
}
void Start() {
characterController = GetComponent<CharacterController>();
currentHealth = maxHealth;
FindObjectOfType<HealthBar>().SetMaxHealth(maxHealth);
// healthBar.SetMaxHealth(maxHealth);
}
void Update() {
if (isLocalPlayer) {
if (characterController.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection *= speed;
if (Input.GetButton("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// if ()
// {
// TakeDamage (10);
// }
void OnCollisionEnter(Collision collision) {
TakeDamage (10);
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(moveDirection);
}
void TakeDamage(int damage) {
currentHealth -= damage;
FindObjectOfType<HealthBar>().SetHealth(currentHealth);
// healthBar.SetHealth(currentHealth);
}
}
public override void OnStartLocalPlayer() {
Camera.main.GetComponent<CameraFollow>().setTarget(gameObject.transform);
}
}
解决方案
移动TakeDamage
和OnCollisionEnter
到类范围。另外我建议暂时删除网络功能。建议的代码如下:
using UnityEngine;
using System.Collections;
using Mirror;
using UnityEngine.UI;
public class Player : Monobehaviour {
CharacterController characterController;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public int maxHealth = 30;
public int currentHealth;
//private HealthBar healthBar;
private Vector3 moveDirection = Vector3.zero;
void Start() {
characterController = GetComponent<CharacterController>();
currentHealth = maxHealth;
FindObjectOfType<HealthBar>().SetMaxHealth(maxHealth);
// healthBar.SetMaxHealth(maxHealth);
}
void Update() {
if (isLocalPlayer) {
if (characterController.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection *= speed;
if (Input.GetButton("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// if ()
// {
// TakeDamage (10);
// }
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(moveDirection);
}
}
void OnCollisionEnter(Collision collision) {
TakeDamage (10);
}
void TakeDamage(int damage) {
currentHealth -= damage;
FindObjectOfType<HealthBar>().SetHealth(currentHealth);
// healthBar.SetHealth(currentHealth);
}
public void Start() {
Camera.main.GetComponent<CameraFollow>().setTarget(gameObject.transform);
}
}
首先从中删除OnCollisionEnter
和,您需要它们在类范围内。TakeDamage
Update
另一方面考虑代码可读性的代码格式。如果格式混乱并且代码不可读,那么您就是在劝阻潜在的帮助者来帮助您!祝你好运
推荐阅读
- javascript - 淡出,更改然后淡入延迟的文本
- java - XDocReport 不是 ImageIO SPI 类
- r - 使用 geom_text 对 ggplot2 中的各个方面进行注释
- java - 我的配置文件出现 PlayerJoinEvent 错误
- c# - C# - 有没有办法在类中调用泛型类型的静态方法?
- html - html电子邮件中的布局
- icalendar - ical/ics:每年进行 3 个月提醒的更好方法
- python - 如何在pygame中画一条向上移动屏幕的线
- android - react-native run-android 错误:Atom 上的 gradlew.bat
- python - 将 K 平均聚类距离度量更改为堪培拉距离或 python 上的任何其他距离度量