首页 > 解决方案 > 如何因与敌人碰撞而损失伤害

问题描述

嗨,我对统一和 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);
    }
}

标签: c#unity3dcollider

解决方案


移动TakeDamageOnCollisionEnter到类范围。另外我建议暂时删除网络功能。建议的代码如下:

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和,您需要它们在类范围内。TakeDamageUpdate

另一方面考虑代码可读性的代码格式。如果格式混乱并且代码不可读,那么您就是在劝阻潜在的帮助者来帮助您!祝你好运


推荐阅读