Raycast 使游戏对象被击中到 运行 脚本并在游戏对象上运行
Raycast to get gameobject being hit to run script and function on gameobject
我正在为我的大学项目让多个敌人在我的游戏中工作。我有一个带有 ShootGun 脚本的播放器,该脚本对枪进行光线投射并检测击中的物体是否是敌方碰撞器之一(标记为 Enemy_Head、_Torso 和 _Limb)。如果是,它将获取敌人命中的游戏对象和该游戏对象上的 enemyHealth 脚本组件,然后调用该脚本上的 public 函数。目前,当 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);
}
}
}
如果您能帮助我找出为什么没有获得参考/设置它们,我们将不胜感激,谢谢。
enHit 可能为空。更换所有
hit.transform.gameObject.GetComponent<EnemyHealth>();
和
hit.collider.gameObject.GetComponent<EnemyHealth>();
您的脚本中大约有 4 个。您想要将 EnemyHealth 脚本附加到射线通过对撞机击中的对象。
编辑:
你还需要改变
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")
这可能无法解决您遇到的当前错误,但这是导致该错误的问题之一。
我正在为我的大学项目让多个敌人在我的游戏中工作。我有一个带有 ShootGun 脚本的播放器,该脚本对枪进行光线投射并检测击中的物体是否是敌方碰撞器之一(标记为 Enemy_Head、_Torso 和 _Limb)。如果是,它将获取敌人命中的游戏对象和该游戏对象上的 enemyHealth 脚本组件,然后调用该脚本上的 public 函数。目前,当 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);
}
}
}
如果您能帮助我找出为什么没有获得参考/设置它们,我们将不胜感激,谢谢。
enHit 可能为空。更换所有
hit.transform.gameObject.GetComponent<EnemyHealth>();
和
hit.collider.gameObject.GetComponent<EnemyHealth>();
您的脚本中大约有 4 个。您想要将 EnemyHealth 脚本附加到射线通过对撞机击中的对象。
编辑:
你还需要改变
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")
这可能无法解决您遇到的当前错误,但这是导致该错误的问题之一。