【问题标题】:How to make the Spheres able to deal damage to eachother?如何使球体能够互相造成伤害?
【发布时间】:2019-08-07 22:25:39
【问题描述】:

想让我的球体能够互相伤害

已尝试获取其他对象的组件

健康脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Hitpoints 
{

    private int health;
    public  int healthMax;

    public Hitpoints(int health)
    {

        this.health = health;

    }

    public int GetHealth()
    {
        return health;
    }

    public float GetHealthPercent()
    {
        return (float)health / healthMax;

    }



    public void Damage(int damageAmount)
    {

        health -= damageAmount;
        if (health < 0)
        {
            health = 0;
        }
    }

    public void Heal(int healAmount)
    {
        health += healAmount;
        if (health > healthMax)
        {
            health = healthMax;
        }

    }
}

伤害脚本

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Detect_Blue : MonoBehaviour
{

    Hitpoints hitpoints = new Hitpoints(100);

    bool onetime = false;
    public float radius;
    public Vector3 direction = new Vector3(2, 2, 2);

    private void Start()
    {


        Debug.Log("Health: " + hitpoints.GetHealth());
        InvokeRepeating("DetectEnemy", 4f, 2f);
    }


    private void DetectEnemy()
    {

        var hitColliders = Physics.OverlapSphere(direction, radius);

        for (var i = 0; i < hitColliders.Length; i++)
        {
            if (hitColliders[i].CompareTag("Player"))
            {
                if (!onetime)
                {

                    onetime = true;
                    print(hitColliders[i].tag);

                    InvokeRepeating("Hello", 1, 1);
                }

            }

// collect information on the hits here
        }

    }

    private void Hello()
    {
        var hitColliders = Physics.OverlapSphere(direction, radius);
        for (var i = 0; i < hitColliders.Length; i++)
        {
            if (hitColliders[i].CompareTag("Player"))
            {
                hitColliders[i].GetComponent<Hitpoints>().Damage(10);
            }




            Debug.Log("Damaged: " +  hitColliders[i].GetComponent<Hitpoints>().GetHealth());
            if (hitpoints.GetHealth() == 0)
            {
                Destroy(gameObject);

            }


        }
    }
}

ArgumentException:GetComponent 要求请求的组件“Hitpoints”派生自 MonoBehaviour 或 Component 或者是一个接口。 UnityEngine.Component.GetComponent[Hitpoints] () (在 C:/buildslave/unity/build/Runtime/Export/Component.bindings.cs:42) Detect_Blue.Hello () (at Assets/Detect_Blue.cs:57)

【问题讨论】:

  • 您想知道错误发生的原因或其他处理问题的方法吗?正如错误消息所说,将 MonoBehaviour 接口添加到 Hitpoint.cs。你也可以使用对撞机和 Oncollisionenter 方法来检测碰撞

标签: c# unity3d


【解决方案1】:

当您对游戏对象执行GetComponent&lt;T&gt; 时,T 的类型应该继承自 monobehaviour。
(因为 Monobehavior 是游戏对象的组件,而您正在尝试获取组件)

要解决此问题,只需使用您已在 Detect_Blue 中创建的 hitpoints

Debug.Log("Damaged: " +  hitColliders[i].GetComponent<Hitpoints>().GetHealth());

会变成

Debug.Log("Damaged: " +  hitColliders[i].GetComponent<Detect_Blue>().hitpoints.GetHealth());

同样对于hitColliders[i].GetComponent&lt;Hitpoints&gt;().Damage(10);,应该是:

hitColliders[i].GetComponent<Detect_Blue>().hitpoints.Damage(10);

或者
您可以简单地让 Hitpoints 从 Monobehavior 继承并将脚本附加到相应的游戏对象。
虽然在你的情况下,第一个解决方案会更可行。


另外,您可以使用OnCollisionEnter 来检测球何时与另一个物体发生碰撞。

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    相关资源
    最近更新 更多