简体   繁体   English

团结生存射击敌人没有受到任何伤害 - Android

[英]Unity Survival Shooter Enemy taking no damage - Android

I am trying to deploy the Unity Survival Shooter game to the android device. 我正在尝试将Unity Survival Shooter游戏部署到Android设备上。 The game works just fine except when I shoot the enemy, it doesn't take any damage and don't die. 游戏工作得很好,除非我射击敌人,它不会受到任何伤害而且不会死亡。 Here is my PlayerShooting script. 这是我的PlayerShooting脚本。

using UnityEngine;
using System.Threading;
using UnityStandardAssets.CrossPlatformInput;

namespace CompleteProject
{
public class PlayerShooting : MonoBehaviour
{
    public int damagePerShot = 20;                  // The damage inflicted by each bullet.
    public float timeBetweenBullets = 0.15f;        // The time between each shot.
    public float range = 100f;                      // The distance the gun can fire.


    float timer;                                    // A timer to determine when to fire.
    Ray shootRay;                                   // A ray from the gun end forwards.
    RaycastHit shootHit;                            // A raycast hit to get information about what was hit.
    int shootableMask;                              // A layer mask so the raycast only hits things on the shootable layer.
    ParticleSystem gunParticles;                    // Reference to the particle system.
    LineRenderer gunLine;                           // Reference to the line renderer.
    AudioSource gunAudio;                           // Reference to the audio source.
    Light gunLight;                                 // Reference to the light component.
    public Light faceLight;                             // Duh
    float effectsDisplayTime = 0.2f;                // The proportion of the timeBetweenBullets that the effects will display for.
    public float timertime = 0.0f;


    void Awake ()
    {
        // Create a layer mask for the Shootable layer.
        shootableMask = LayerMask.GetMask ("Shootable");

        // Set up the references.
        gunParticles = GetComponent<ParticleSystem> ();
        gunLine = GetComponent <LineRenderer> ();
        gunAudio = GetComponent<AudioSource> ();
        gunLight = GetComponent<Light> ();
        //faceLight = GetComponentInChildren<Light> ();
    }


    void Update ()
    {
        // Add the time since Update was last called to the timer.
        timer += Time.deltaTime;

     #if !MOBILE_INPUT
        // If the Fire1 button is being press and it's time to fire...
        if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
        {
            // ... shoot the gun.
            Shoot ();
        }
       #else
        // If there is input on the shoot direction stick and it's time to fire.
        bool check = CrossPlatformInputManager.GetButtonDown("JumpButton");
        check = true;
        //Debug.Log(check);
        if(check == true)
       // if ((CrossPlatformInputManager.GetAxisRaw("Move X") != 0 || CrossPlatformInputManager.GetAxisRaw("Move Y") != 0) && timer >= timeBetweenBullets)
        {
            // ... shoot the gun
            //System.Threading.Thread.Sleep(2000);
            timertime -= Time.deltaTime;
            if(timertime < 0.0f)
            {
                Shoot();
                timertime = 0.175f;
            }
        }
        #endif
        // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
        if(timer >= timeBetweenBullets * effectsDisplayTime)
        {
            // ... disable the effects.
            DisableEffects ();
        }
    }


    public void DisableEffects ()
    {
        // Disable the line renderer and the light.
        gunLine.enabled = false;
        faceLight.enabled = false;
        gunLight.enabled = false;
    }


    public void Shoot ()
    {
        // Reset the timer.
        Debug.Log("Inside Shoot");
        timer = 0f;

        // Play the gun shot audioclip.
        gunAudio.Play ();

        // Enable the lights.
        gunLight.enabled = true;
        faceLight.enabled = true;

        // Stop the particles from playing if they were, then start the particles.
        gunParticles.Stop ();
        gunParticles.Play ();

        // Enable the line renderer and set it's first position to be the end of the gun.
        gunLine.enabled = true;
        gunLine.SetPosition (0, transform.position);

        // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
        shootRay.origin = transform.position;
        shootRay.direction = transform.forward;

        // Perform the raycast against gameobjects on the shootable layer and if it hits something...
        Debug.Log("Going inside Physics Raycast");
        if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
        {
            // Try and find an EnemyHealth script on the gameobject hit.
            Debug.Log("Inside Physics Raycast");
            EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
            Debug.Log (enemyHealth);
            Debug.Log ("Checking if enemyHealth is not null");

            // If the EnemyHealth component exist...
            if(enemyHealth != null)
            {
                // ... the enemy should take damage.
                Debug.Log("Inside enemyHealth != null if condition");
                enemyHealth.TakeDamage (damagePerShot, shootHit.point);
            }

            // Set the second position of the line renderer to the point the raycast hit.
            gunLine.SetPosition (1, shootHit.point);
        }
        // If the raycast didn't hit anything on the shootable layer...
        else
        {
            // ... set the second position of the line renderer to the fullest extent of the gun's range.
            gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
        }
    }
}
}

And here is my EnemyHealth script. 这是我的EnemyHealth脚本。

using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace CompleteProject
{
public class EnemyHealth : 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 ();

        // 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 <UnityEngine.AI.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);
    }
}
}

I have put the log everywhere and found that the enemyHealth object from PlayerShooting script is always returning Null even though if I hit the enemy with bullets. 我把日志放在各处,发现即使我用子弹击中了敌人,PlayerShooting脚本中的敌人健康对象也总是返回Null。

PlayerShooting脚本截图

Unity截图

Need to know why it is always returning Null and how do I make it other than Null when I hit the enemy. 需要知道为什么它总是返回Null,当我击中敌人时如何使它除了Null之外。

You are already using Layer to make sure that the raycast hit just the GameObjects on the shootableMask layer which is the "Shootable" layer so layer issue problem is eliminated. 您已经在使用Layer来确保光线投射仅触及shootableMask图层上的GameObjects,这是“可拍摄”图层,因此消除了图层问题。

The only possible problem left is that the EnemyHealth script is not attached to the GameObject or maybe some of the GameObjects. 剩下的唯一可能的问题是EnemyHealth脚本没有附加到EnemyHealth或者某些GameObjects。

Go through all your GameObjects and prefabs, select each one on the "Shootable" layer then make sure that the EnemyHealth script is attached to them . 浏览所有GameObjects和prefabs,在“Shootable”图层上选择每个,然后确保EnemyHealth脚本附加到它们 The GetComponent <EnemyHealth>() should never be null if the EnemyHealth script is attached to each one. 如果将EnemyHealth脚本附加到每个脚本,则GetComponent <EnemyHealth>()永远不应为null

You should change the Getcomponent<> function into FindObjectOfType because Getcomponent only gets the component from that game object, in this situation the EnemyHealth Script doesn't exist in same object with PlayerShooting so you cant get the component. 您应该将Getcomponent<>函数更改为FindObjectOfType因为Getcomponent仅从该游戏对象获取组件,在这种情况下, EnemyHealth脚本与PlayerShooting不存在于同一对象中,因此您无法获取该组件。 Read more about FindObjectOfType 阅读有关FindObjectOfType更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM