Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Zombie damage Problem [SOLVED]

Discussion in 'Scripting' started by Animator, Oct 4, 2016.

  1. Animator

    Animator

    Joined:
    Apr 6, 2012
    Posts:
    38
    Hey guys
    ok so my script works perfectly fine when i have only one zombie on screen. but now that i implemented a spawner, i realised, that im checking just for any rigidbody. so if the raycast hits SOME rigidbody, it acceses my player script and -damage. equals my zombies hit eachother and take my health. so i have to somehow check if a rigidbody AND the tag player is = true and then execute the step of taking damage. i tryed playing around with built in layers but if i make zombies ignoreRaycast then my shooting obviuously wont work, so i played around with custom layers but since im new to coding i cant get it to work. wonder if someone could help me fix this
    thanks in advance
    greetings ilias

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enemyScript: MonoBehaviour {
    5.  
    6.     public int evilHealth = 100;
    7.     public int speed = 5;
    8.     public float attackRange = 1.5f;
    9.     public float stopDist = 1f;
    10.     public int damage = 10;
    11.     public int force = 300;
    12.     public float attackRate = 0.5f;
    13.     public float lastShot;
    14.     public float timeBetweenHits = 1f;
    15.  
    16.     private Transform player;
    17.     private float timeSinceLastHit;
    18.  
    19.     // Use this for initialization
    20.     void Start ()
    21.     {
    22.         player = GameObject.FindGameObjectWithTag ("Player").transform;
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update ()
    27.     {
    28.         //AI
    29.         transform.Translate(0, 0, speed * Time.deltaTime);
    30.         transform.LookAt (player);
    31.  
    32.         //health
    33.         if (evilHealth <= 0)
    34.         {
    35.             Destroy (gameObject);
    36.         }
    37.     }
    38.  
    39.     void FixedUpdate ()
    40.     {
    41.         timeSinceLastHit += Time.deltaTime;
    42.         if (timeSinceLastHit >= timeBetweenHits)
    43.         {
    44.             attack ();
    45.             timeSinceLastHit = 0f;
    46.         }
    47.  
    48.         //distance check
    49.         RaycastHit hit;
    50.         Vector3 fwd = transform.TransformDirection (Vector3.forward);
    51.  
    52.         if (Physics.Raycast (transform.position, fwd, out hit))
    53.         {
    54.             if (hit.distance <= stopDist)
    55.             {
    56.                 speed = 0;
    57.             }
    58.             else
    59.             {
    60.                 speed = 6;
    61.             }
    62.         }
    63.     }
    64.  
    65.     void attack ()
    66.     {
    67.         RaycastHit hit;
    68.         Vector3 fwd = transform.TransformDirection (Vector3.forward);
    69.         Debug.DrawRay (transform.position, fwd * attackRange, Color.red);
    70.  
    71.         if (Physics.Raycast (transform.position, fwd, out hit, attackRange))
    72.         {
    73.             if (hit.collider)
    74.             {
    75.                
    76.             }
    77.  
    78.             if (hit.rigidbody)
    79.             {
    80.                 GameObject.FindWithTag ("Player").GetComponent <playerScript> ().currentHealth -= damage;
    81.                 hit.rigidbody.AddForceAtPosition (fwd * force, hit.point);      
    82.             }
    83.         }
    84.     }
    85. }
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Can't you do if (hit.rigidbody.tag == "ZombieOrPlayerOrWhateverYouWant")?
     
  3. Animator

    Animator

    Joined:
    Apr 6, 2012
    Posts:
    38
    omg yes! such a simple line..i tryed all this rigidbody.GameObject.FindWithTag and all thats stuff but nothing worked, didnt know that there is such a simple way :) thanks alot, this saved me alot of time with that layer stuff :)
    greetings Ilias
     
    DroidifyDevs likes this.
  4. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    GameObject.CompareTag should be used to compare tags.

    You could also replace GameObject.FindWithTag for a more efficient method.. PlayerScript should have a function for "ApplyDamage" which reduces the players health. You could also pass in the relevant data for "AddForceAtPosition" as parameters on the ApplyDamage function as well... This would completely negate the need for you to check for a rigidbody in your attack function and instead do it in a PlayerScript.ApplyDamage function where it belongs.

    Example:
    Code (csharp):
    1. if (Physics.Raycast(transform.position, fwd, out hit, attackRange)
    2. {
    3.    if (hit.transform.gameObject.CompareTag("Player"))
    4.    {
    5.       PlayerScript player = hit.transform.gameObject.GetComponent<PlayerScript>();
    6.       if (player != null)
    7.       {
    8.          player.ApplyDamage(damage, fwd * force, hit.point);
    9.       }
    10.    }
    11. }
    P.S. Why are you checking health every update??? You should have functions that you use to apply damage and destroy your object if health is less than one...
     
    Last edited: Oct 4, 2016