【问题标题】:Survival Shooter unity enemies wont take damage生存射击团结敌人不会受到伤害
【发布时间】:2019-02-26 14:40:11
【问题描述】:

我正在为团结做生存射击教程,我刚刚添加了使敌人受到玩家枪支伤害的脚本。我将脚本添加到敌人身上,但每当我玩它时,他们都不会受到伤害

我在控制台中没有收到任何错误或警告,它们仍然可以攻击我并正常移动,但它们不会受到伤害,子弹会直接穿过它们

using UnityEngine;
using UnityEngine.AI;

public class EnemyDamage : MonoBehaviour
{
    public int startingHealth = 100;           // The amount of health the enemy starts the game with.
    public int currentHealth;                  // The current health the enemy has.
    public float sinkSpeed = 2.5f;             // The speed at which the enemy sinks through the floor when dead.
    public int scoreValue = 10;                // The amount added to the player's score when the enemy dies.
    public AudioClip deathClip;                // The sound to play when the enemy dies.


    Animator anim;                             // Reference to the animator.
    AudioSource enemyAudio;                    // Reference to the audio source.
    ParticleSystem hitParticles;               // Reference to the particle system that plays when the enemy is damaged.
    CapsuleCollider capsuleCollider;           // Reference to the capsule collider.
    bool isDead;                               // Whether the enemy is dead.
    bool isSinking;                            // Whether the enemy has started sinking through the floor.


    void Awake()
    {
        // Setting up the references.
        anim = GetComponent<Animator>();
        enemyAudio = GetComponent<AudioSource>();
        hitParticles = GetComponentInChildren<ParticleSystem>();
        capsuleCollider = GetComponent<CapsuleCollider>();

        // Setting the current health when the enemy first spawns.
        currentHealth = startingHealth;
    }

    void Update()
    {
        // If the enemy should be sinking...
        if (isSinking)
        {
            // ... move the enemy down by the sinkSpeed per second.
            transform.Translate(-Vector3.up * sinkSpeed * Time.deltaTime);
        }
    }


    public void TakeDamage(int amount, Vector3 hitPoint)
    {
        // If the enemy is dead...
        if (isDead)
        // ... no need to take damage so exit the function.
        return;

        // Play the hurt sound effect.
        enemyAudio.Play();
        Debug.Log("damage");

        // Reduce the current health by the amount of damage sustained.
        currentHealth -= amount;

        // Set the position of the particle system to where the hit was sustained.
        hitParticles.transform.position = hitPoint;

        // And play the particles.
        hitParticles.Play();

        // If the current health is less than or equal to zero...
        if (currentHealth <= 0)
        {
            // ... the enemy is dead.
            Death();
        }
    }


    void Death()
    {
        // The enemy is dead.
        isDead = true;

        // Turn the collider into a trigger so shots can pass through it.
        capsuleCollider.isTrigger = true;

        // Tell the animator that the enemy is dead.
        anim.SetTrigger("Dead");

        // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
        enemyAudio.clip = deathClip;
        enemyAudio.Play();
    }


    public void StartSinking()
    {
        // Find and disable the Nav Mesh Agent.
        GetComponent<NavMeshAgent>().enabled = false;

        // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
        GetComponent<Rigidbody>().isKinematic = true;

        // The enemy should no sink.
        isSinking = true;

        // Increase the score by the enemy's score value.
        ScoreManager.score += scoreValue;

        // After 2 seconds destory the enemy.
        Destroy(gameObject, 2f);
    }
}

【问题讨论】:

  • ...那是因为敌人非常擅长生存...对不起,请忽略我... ;-)
  • 顺便说一句,对于想要将您的代码内联到您的问题中的人来说,这真的很不方便。要求在该网站上创建/注册一个帐户只是为了能够获得粘贴/代码的纯文本版本,这简直是天方夜谭......
  • Unity的教程很全,你可能漏掉了一步,再过一遍,确保没有漏掉任何东西。
  • 你从哪里调用 TakeDamage?
  • 你在角色上缺少Rigid Body吗?

标签: c# unity3d


【解决方案1】:

您可能缺少enemies、bullets 或both 的层或标签。还要检查project settings -&gt; physic中的层碰撞矩阵

【讨论】:

  • 这不应归类为答案。 OP 下​​的 cmets 是提供建议的好地方。
  • @Daniel 真正的问题是这不是一个有效的问题
  • @Jichael 一切都已检查
猜你喜欢
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
相关资源
最近更新 更多