问题描述
嗨,我对 unity 和 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);
}
}
解决方法
将 const importTables = [
"Macquarie University course.csv","Australia National University courses.csv","UTS courses.csv","UNSW courses.csv","Western University of Sydney courses.csv"
]
const Importdatamultiplefiles = async function () {
await Promise.all(importTables.map(CourseInformation => importdata(CourseInformation,courseParams.TableName)));
return;
}
Importdatamultiplefiles();
和 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
,您需要它们在类范围内。
另一方面要考虑代码格式以提高代码可读性。如果格式混乱且代码不可读,您就会劝阻潜在的帮助者来帮助您!祝你好运