C# 如何在与敌人的碰撞中损失伤害

C# 如何在与敌人的碰撞中损失伤害,c#,unity3d,collider,C#,Unity3d,Collider,嗨,我是unity和C#的新手,非常感谢您的帮助!我正试图让我的敌人(预制场名为雪人)和我的玩家(预制场名为圣诞老人)发生碰撞。当这种情况发生时,玩家应该在其healthbar中损失伤害。healthbar出现了,但当它与雪人相撞时,如何让它失去健康,我真是太迷茫了 以下是脚本: using UnityEngine; using System.Collections; using Mirror; using UnityEngine.UI; public class Player : Netwo

嗨,我是unity和C#的新手,非常感谢您的帮助!我正试图让我的敌人(预制场名为雪人)和我的玩家(预制场名为圣诞老人)发生碰撞。当这种情况发生时,玩家应该在其healthbar中损失伤害。healthbar出现了,但当它与雪人相撞时,如何让它失去健康,我真是太迷茫了

以下是脚本:

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);
    }
}
使用UnityEngine;
使用系统集合;
使用镜子;
使用UnityEngine.UI;

公共类玩家:网络行为{ 字符控制器字符控制器; 公共浮子速度=6.0f; 公共浮子跳跃速度=8.0f; 公共浮子重力=20.0f; 公共健康指数=30; 公共卫生; //私人健康酒吧; 专用矢量3移动方向=矢量3.0; 无效伤害(内部伤害){ 当前健康-=损害; FindObjectOfType().SetHealth(currentHealth); //healthBar.SetHealth(currentHealth); } void Start(){ characterController=GetComponent(); currentHealth=maxHealth; FindObjectOfType().SetMaxHealth(maxHealth); //healthBar.SetMaxHealth(maxHealth); } 无效更新(){ if(IsLocalLayer){ if(characterController.IsGround){ //我们停飞了,所以重新计算 //直接从轴移动方向 moveDirection=新矢量3(Input.GetAxis(“水平”),0.0f,Input.GetAxis(“垂直”); 移动方向*=速度; if(Input.GetButton(“跳转”)){ 移动方向。y=跳跃速度; } } //如果() // { //损伤(10); // } 无效碰撞中心(碰撞){ 损伤(10); } moveDirection.y-=重力*时间增量; characterController.Move(moveDirection*Time.deltaTime); transform.rotation=Quaternion.LookRotation(移动方向); } 无效伤害(内部伤害){ 当前健康-=损害; FindObjectOfType().SetHealth(currentHealth); //healthBar.SetHealth(currentHealth); } } 公共覆盖无效OnStartLocalPlayer(){ Camera.main.GetComponent().setTarget(gameObject.transform); } }
TakeDamage
OnCollisionEnter
移动到类范围。我还建议暂时删除网络功能。建议的meve代码如下:

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);
    }
}
使用UnityEngine;
使用系统集合;
使用镜子;
使用UnityEngine.UI;
公共类玩家:单一行为{
字符控制器字符控制器;
公共浮子速度=6.0f;
公共浮子跳跃速度=8.0f;
公共浮子重力=20.0f;
公共健康指数=30;
公共卫生;
//私人健康酒吧;
专用矢量3移动方向=矢量3.0;
void Start(){
characterController=GetComponent();
currentHealth=maxHealth;
FindObjectOfType().SetMaxHealth(maxHealth);
//healthBar.SetMaxHealth(maxHealth);
}
无效更新(){
if(IsLocalLayer){
if(characterController.IsGround){
//我们停飞了,所以重新计算
//直接从轴移动方向
moveDirection=新矢量3(Input.GetAxis(“水平”),0.0f,Input.GetAxis(“垂直”);
移动方向*=速度;
if(Input.GetButton(“跳转”)){
移动方向。y=跳跃速度;
}
}
//如果()
//  {
//损伤(10);
//  }
moveDirection.y-=重力*时间增量;
characterController.Move(moveDirection*Time.deltaTime);
transform.rotation=Quaternion.LookRotation(移动方向);
}
}
无效碰撞中心(碰撞){
损伤(10);
}
无效伤害(内部伤害){
当前健康-=损害;
FindObjectOfType().SetHealth(currentHealth);
//healthBar.SetHealth(currentHealth);
}
公开作废开始(){
Camera.main.GetComponent().setTarget(gameObject.transform);
}
}
首先,从
Update
中删除
onCollisionCenter
TakeDamage
,您需要它们在类范围内


另一方面考虑到代码格式对代码可读性的影响。如果格式凌乱且代码不可读,则会阻止潜在的帮助者帮助您!祝你好运

你所做的有什么不对吗?i、 e.
TakeDamage
代码编译了吗?@rustyBucketBay它确实编译了,但是当我玩游戏和触摸雪人时,我的身体没有任何变化healthbar@MickyD当我玩游戏,玩家触摸雪人时,HealthBar什么也没发生为什么你的方法嵌套在
Update
方法中?!我已经在private movement变量下面的类作用域中添加了这些变量,但现在我有一个错误,即“TakeDamage”在当前上下文中不存在。我想知道你是否知道一种更简单的编码方法,当我与敌人相撞时,我会受到伤害?感谢迄今为止所有的帮助-是的,我一定会在代码格式方面下工夫。请在问题“公共类玩家:网络行为”中发布代码{CharacterController CharacterController;公共浮点速度=6.0f;公共浮点跳跃速度=8.0f;公共浮点重力=20.0f;公共int maxHealth=30;公共int currentHealth;//私有HealthBar HealthBar;私有向量3 moveDirection=向量3.0;无效OnCollisionCenter(碰撞){TakeDamage(10);}void Start(){对不起,我不知道该怎么做