在生存射击游戏中,我有敌人的对象并添加了我自己的3d游戏对象 一切正常,游戏对象跟随并伤害玩家,但是当玩家射击游戏对象时,子弹穿过游戏对象并且分数也不会增加。我已经将刚体组件附加到我的自定义 3d 游戏对象上,但子弹仍然没有造成任何损坏。
This is the code for PlayerShooting
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public float range = 100f;
float timer;
Ray shootRay;
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
AudioSource gunAudio;
Light gunLight;
float effectsDisplayTime = 0.2f;
void Awake ()
{
shootableMask = LayerMask.GetMask ("Default");
shootableMask = LayerMask.GetMask ("Default");
shootableMask = LayerMask.GetMask("Default");
shootableMask = LayerMask.GetMask("Default");
gunParticles = GetComponent<ParticleSystem> ();
gunLine = GetComponent <LineRenderer> ();
gunAudio = GetComponent<AudioSource> ();
gunLight = GetComponent<Light> ();
}
void Update ()
{
timer += Time.deltaTime;
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets)
{
Shoot ();
}
if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}
public void DisableEffects ()
{
gunLine.enabled = false;
gunLight.enabled = false;
}
void Shoot ()
{
timer = 0f;
gunAudio.Play ();
gunLight.enabled = true;
gunParticles.Stop ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
下面是敌人的生命值脚本:
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public float sinkSpeed = 2.5f;
public int scoreValue = 10;
public AudioClip deathClip;
Animator anim;
AudioSource enemyAudio;
ParticleSystem hitParticles;
CapsuleCollider capsuleCollider;
bool isDead;
bool isSinking;
void Awake ()
{
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
currentHealth = startingHealth;
}
void Update ()
{
if(isSinking)
{
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
}
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
if(isDead)
return;
enemyAudio.Play ();
currentHealth -= amount;
hitParticles.transform.position = hitPoint;
hitParticles.Play();
if(currentHealth <= 0)
{
Death ();
}
}
void Death ()
{
isDead = true;
capsuleCollider.isTrigger = true;
anim.SetTrigger ("Dead");
ScoreManager.score += scoreValue;
enemyAudio.clip = deathClip;
enemyAudio.Play ();
}
public void StartSinking ()
{
GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;
GetComponent <Rigidbody> ().isKinematic = true;
isSinking = true;
ScoreManager.score += scoreValue;
Destroy (gameObject, 2f);
}
}
如果显示子弹的线穿过敌方游戏对象,则您的 Physics.Raycast没有击中此游戏对象的碰撞体。
您可以通过向光线投射块添加一些调试输出来快速测试这一点,如下所示:
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
Debug.Log("I HIT SOMETHING CALLED: " + shootHit.collider.gameObject.name);
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
Debug.Log("I DIDN'T HIT ANYTHING!");
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
我怀疑您的问题是您的敌方游戏对象位于默认层上。
在 Awake 方法中,您将按如下方式设置可拍摄蒙版shootableMask = LayerMask.GetMask ("Default");
然后当您调用Physics.Raycast
时,您将传递它 shootableMask。 这实际上是告诉光线投射忽略默认图层上的任何对象。
尝试将敌人的游戏对象放在不同的图层上。
附带说明一下,您只需要在 Awake 方法中使用一次shootableMask = LayerMask.GetMask ("Default");
,而不是四次。