【问题标题】:Raycast to get gameobject being hit to run script and function on gameobjectRaycast 让游戏对象被击中以在游戏对象上运行脚本和函数
【发布时间】:2023-04-02 16:31:01
【问题描述】:

我正在让多个敌人在我的游戏中为我的大学项目工作。我有一个带有 ShootGun 脚本的玩家,该脚本对枪进行射线投射并检测被击中的物体是否是敌对撞机之一(标记为 Enemy_Head、_Torso 和 _Limb)。如果是,它将获取被击中的敌人的游戏对象和该游戏对象上的enemyHealth 脚本组件,然后将调用该脚本上的公共函数。目前,当 ShootGun 脚本尝试获取组件/脚本时,它会显示以下错误:

NullReferenceException:对象引用未设置为对象的实例 ShootGun.gunFire () (在 Assets/Scripts/Gun/ShootGun.cs:107)

更新的错误代码:

NullReferenceException:对象引用未设置为对象的实例 ShootGun.gunFire () (在 Assets/Scripts/Gun/ShootGun.cs:113)

第 113 行 = enHit.enemyShotTorso();

enHit 尝试从enemyHealth 脚本调用函数的每一行都会出现错误。

以下是我的 ShootGun 脚本中的光线投射:

// Raycasting for bullet projection against obstacles within the world (WIP)
            float gunRayDistance = 50f;

            Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));

            // Name what for the raycast collides with (used to reference the target point)
            RaycastHit hit;

            // The actual raycast
            if(Physics.Raycast(ray, out hit, gunRayDistance, 1 << 9) || Physics.Raycast(ray, out hit, gunRayDistance, 1 << 8)) {
                Debug.Log("Bullet Hit");

                EnemyHealth enHit = hit.collider.gameObject.GetComponent<EnemyHealth>();

                // Checking if the raycast (bullet) collided with objects tagged with "Enemy_Head".
                if (hit.collider.gameObject.CompareTag("Enemy_Head")) {
                    Debug.Log ("Headshot!");
                    //hitPoint = hit.collider.gameObject;
                    enHit = hit.collider.gameObject.GetComponent<EnemyHealth>();
                    enHit.enemyShotHead();
                }
                // Checking if the raycast (bullet) collided with objects tagged with "Enemy_Torso".
                if (hit.collider.gameObject.CompareTag("Enemy_Torso")) {
                    Debug.Log ("Body-shot!");
                    //hitPoint = hit.collider.gameObject;
                    enHit = hit.collider.gameObject.GetComponent<EnemyHealth>();
                    enHit.enemyShotTorso();
                }
                // Checking if the raycast (bullet) collided with objects tagged with "Enemy_Limb".
                if (hit.collider.gameObject.CompareTag("Enemy_Limb")) {
                    Debug.Log ("Limb-shot!");
                    enHit = hit.collider.gameObject.GetComponent<EnemyHealth>();
                    enHit.enemyShotLimb();
                }
                // The point of contact with the model is given by the hit.point (to not cause z-fighting issues with layering)
                Vector3 bulletHolePosition = hit.point + hit.normal * 0.01f;
                // Rotation to match where it hits (between the quad vector forward axis and the hit normal)
                Quaternion bulletHoleRotation = Quaternion.FromToRotation(-Vector3.forward, hit.normal);
                GameObject hole = (GameObject)GameObject.Instantiate(bulletHolePrefab, bulletHolePosition, bulletHoleRotation);
                // Destroy the instantiated gameobject of the bulletHole after a delay of 5 seconds.
                Destroy (hole, 5.0f);
            }                                                                                                                    
        }

以下是我的 EnemyHealth 脚本:

public class EnemyHealth : MonoBehaviour {

public float enemyHealth = 100.0f;

// Use this for initialization
void Start () {

}
// Update is called once per frame
void Update () {
    enemyDeath ();
}

public void enemyShotHead() {
    enemyHealth -= 60f;
    Debug.Log (enemyHealth);
}

public void enemyShotTorso() {
    enemyHealth -= 40f;
    Debug.Log (enemyHealth);
}

public void enemyShotLimb() {
    enemyHealth -= 20f;
    Debug.Log (enemyHealth);
}

void enemyDeath() {
    if (enemyHealth <= 0.0f) {
        Debug.Log ("Enemy Killed");
        gameObject.SetActive(false);
    }
}

}

任何帮助试图找出为什么它没有得到参考/设置它们将不胜感激,谢谢。

【问题讨论】:

  • ShootGun.cs 的第 107 行出现运行时错误。你能把代码贴在那行吗?
  • 107 是这一行:enHit.enemyShotTorso();在 Enemy_Torso 下检查 if 语句

标签: c# unity3d reference raycasting


【解决方案1】:

enHit 可能为空。替换你所有的

hit.transform.gameObject.GetComponent<EnemyHealth>();

hit.collider.gameObject.GetComponent<EnemyHealth>();

您的脚本中有大约 4 个。您希望将 EnemyHealth Script 附加到 Ray 通过对撞机击中的对象上。

编辑:

你也需要改变

hit.transform.CompareTag("Enemy_Head")
hit.transform.CompareTag("Enemy_Torso")
hit.transform.CompareTag("Enemy_Limb")

hit.collider.gameObject.CompareTag("Enemy_Head")
hit.collider.gameObject.CompareTag("Enemy_Torso")
hit.collider.gameObject.CompareTag("Enemy_Limb")

这可能无法解决您当前遇到的错误,但它是导致该错误的问题之一。

【讨论】:

  • 我已经尝试过了,但我需要调用该脚本上的特定函数来处理正确的伤害量(取决于身体部位的命中)。 enHint /should/ 设置为 hit.collider.gameObject.GetComponent() 它似乎没有将其检测为参考?
  • 替换 enHit = hit.transform.gameObject.GetComponent();与 enHit =hit.collider.gameObject.GetComponent();然后发布您遇到的错误。在你这样做之前,编辑你的问题并用我所说的更新你的代码。
  • 更新了代码。错误:NullReferenceException:对象引用未设置为对象的实例 ShootGun.gunFire ()(在 Assets/Scripts/Gun/ShootGun.cs:113)第 113 行 = enHit.enemyShotTorso(); enHit 尝试从enemyHealth 脚本调用函数的每一行都会出现错误。
  • 我更新了我的答案。这可能无法解决问题,但这是您遇到的问题之一。请再次更新您的代码并报告发生的情况。顺便问一下,你的敌人脚本附在什么上面?你的猎枪脚本附在什么上面?您是否在 Ray ray = GetComponent ().ViewportPointToRay (new Vector3 (0.5F, 0.5F, 0)); 行上收到有关相机的任何错误?
  • 编辑了我的代码,同样的错误仍在发生。我的 EnemyHealth 脚本附在我的每个敌人身上,分散在地图上(我目前有 5 个)。我的 ShootGun 脚本附加到连接到我的玩家角色的相机上。我没有收到来自 Ray ray = GetComponent... 行的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多