Search Unity

Attack script help please

Discussion in 'Scripting' started by keepthachange, Nov 13, 2015.

  1. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    sls
     
    Last edited: Nov 13, 2015
  2. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    Don't destroy the gameobject - using again Instantiate you unnecessarily workload memory. Create hit gameobject on Awake or Start function end hide it using gameObject.SetActive(false);
    When hit object will be need just send SetActive(true) and will be show.
    Hide object after collision is simple so I don't explained.
    To active object again I wouldn't use WaitForSeconds(); only something related with animation. Maybe when animation is ended?!
    Code (csharp):
    1. if ( !animation.IsPlaying("hitobject")) // when anim with collider will finish
    2. {
    3.    hit.SetActive(true);
    4. }
     
  3. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    I'm not following? im sorry
     
  4. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    I not browsed your script, I only answered the question. What you don't understand?
    Let's start form load scene: Your character have sword with cube collider right?! And the cube should be hide yes?
     
  5. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    yes there is a cube on my players sword, i have animated it with the sword in maya when it hits the player i want it only hit once then disable itself and go back on after a moment or two
     
  6. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Yeah I get what @rchmiel is saying. What he's saying is just to make a reference to the collider and turn it off, then turn it on again. He was also saying that you don't need to do the Instantiate(), and the Destroy(), as you can just do gameObject.SetActive(false), and gameObject.SetActive(true). But there's an easier way here

    Code (CSharp):
    1.     private void CharacterHit()
    2.     {
    3.         StartCoroutine(TemporaryDisableHit(0.2f));
    4.     }
    5.  
    6.     private IEnumerator TemporaryDisableHit(float duration)
    7.     {
    8.         Collider objectsCollider = GetComponent<Collider>();
    9.  
    10.         objectsCollider.enabled = false;
    11.  
    12.         yield return new WaitForSeconds(duration);
    13.  
    14.         objectsCollider.enabled = true;
    15.     }
    Just call this Coroutine whenever you want to turn off the collider, and choose the amount of time.. The only thing you may want to change is the Collider in the Coroutine.. So maybe someobject.GetComponent<Collider>().. and change the duration, but asides that. All good :).
     
  7. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    how would i add what you said to this script or do i make new scirpt? what exactly am i puting in the script and where? lol so basicly everything i do not understand i did not make this script
     
  8. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    So just reference the collider to whatever you want to, like I have with this Cube:

    Code (CSharp):
    1.     public GameObject cube;
    2.  
    3.     private void CharacterHit()
    4.     {
    5.         StartCoroutine(TemporaryDisableHit(0.2f));
    6.     }
    7.  
    8.     private IEnumerator TemporaryDisableHit(float duration)
    9.     {
    10.         Collider objectsCollider = cube.GetComponent<Collider>();
    11.  
    12.         objectsCollider.enabled = false;
    13.  
    14.         yield return new WaitForSeconds(duration);
    15.  
    16.         objectsCollider.enabled = true;
    17.     }
     
  9. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    so i put this on the cube itself? how does it now what its hitting like this is for my player that attacks enemy
     
  10. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Literally just like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BulletStatusC : MonoBehaviour
    5. {
    6.  
    7.     public int damage = 10;
    8.     public int damageMax = 20;
    9.  
    10.     private int playerAttack = 5;
    11.     public int totalDamage = 0;
    12.     public int variance = 15;
    13.     public string shooterTag = "Player";
    14.  
    15.     [HideInInspector]
    16.     public GameObject shooter;
    17.  
    18.     public Transform Popup;
    19.  
    20.     public GameObject hitEffect;
    21.     public bool flinch = false;
    22.     public bool penetrate = false;
    23.     private int popDamage = 0;
    24.  
    25.     public enum AtkType
    26.     {
    27.         Physic = 0,
    28.         Magic = 1,
    29.     }
    30.  
    31.     public AtkType AttackType = AtkType.Physic;
    32.  
    33.     public enum Elementala
    34.     {
    35.         Normal = 0,
    36.         Fire = 1,
    37.         Ice = 2,
    38.         Earth = 3,
    39.         Lightning = 4,
    40.     }
    41.  
    42.     public Elementala element = Elementala.Normal;
    43.  
    44.     void Start()
    45.     {
    46.         if (variance >= 100)
    47.         {
    48.             variance = 100;
    49.         }
    50.         if (variance <= 1)
    51.         {
    52.             variance = 1;
    53.         }
    54.  
    55.     }
    56.  
    57.     public void Setting(int str, int mag, string tag, GameObject owner)
    58.     {
    59.         if (AttackType == AtkType.Physic)
    60.         {
    61.             playerAttack = str;
    62.         }
    63.         else
    64.         {
    65.             playerAttack = mag;
    66.         }
    67.         shooterTag = tag;
    68.         shooter = owner;
    69.         int varMin = 100 - variance;
    70.         int varMax = 100 + variance;
    71.         int randomDmg = Random.Range(damage, damageMax);
    72.         totalDamage = (randomDmg + playerAttack) * Random.Range(varMin, varMax) / 100;
    73.     }
    74.  
    75.     void OnTriggerEnter(Collider other)
    76.     {
    77.         StartCoroutine(TemporaryDisableHit(0.2f));
    78.         Transform dmgPop;
    79.         Vector3 dir;
    80.  
    81.         //When Player Shoot at Enemy        
    82.         if (shooterTag == "Player" && other.tag == "Enemy")
    83.         {
    84.             dmgPop = (Transform)Instantiate(Popup, other.transform.position, transform.rotation);
    85.  
    86.             if (AttackType == AtkType.Physic)
    87.             {
    88.                 popDamage = other.GetComponent<StatusC>().OnDamage(totalDamage, (int)element);
    89.             }
    90.             else
    91.             {
    92.                 popDamage = other.GetComponent<StatusC>().OnMagicDamage(totalDamage, (int)element);
    93.             }
    94.  
    95.             if (popDamage < 1)
    96.             {
    97.                 popDamage = 1;
    98.             }
    99.  
    100.             if (shooter && shooter.GetComponent<ShowEnemyHealthC>())
    101.             {
    102.                 shooter.GetComponent<ShowEnemyHealthC>().GetHP(other.GetComponent<StatusC>().maxHealth, other.gameObject, other.name);
    103.             }
    104.  
    105.             dmgPop.GetComponent<DamagePopupC>().damage = popDamage;
    106.  
    107.             if (hitEffect)
    108.             {
    109.                 Instantiate(hitEffect, transform.position, transform.rotation);
    110.             }
    111.  
    112.             if (flinch)
    113.             {
    114.                 dir = (other.transform.position - transform.position).normalized;
    115.                 other.GetComponent<StatusC>().Flinch(dir);
    116.             }
    117.  
    118.             if (!penetrate)
    119.             {
    120.                 Destroy(gameObject);
    121.             }
    122.  
    123.             //When Enemy Shoot at Player
    124.         }
    125.         else if (shooterTag == "Enemy" && other.tag == "Player" || shooterTag == "Enemy" && other.tag == "Ally")
    126.         {
    127.  
    128.             //dmgPop = (Transform)Instantiate(Popup, other.transform.position , transform.rotation);
    129.  
    130.             if (AttackType == AtkType.Physic)
    131.             {
    132.                 popDamage = other.GetComponent<StatusC>().OnDamage(totalDamage, (int)element);
    133.             }
    134.             else
    135.             {
    136.                 popDamage = other.GetComponent<StatusC>().OnMagicDamage(totalDamage, (int)element);
    137.             }
    138.  
    139.             dmgPop = (Transform)Instantiate(Popup, transform.position, transform.rotation);
    140.  
    141.             if (popDamage < 1)
    142.             {
    143.                 popDamage = 1;
    144.             }
    145.  
    146.             dmgPop.GetComponent<DamagePopupC>().damage = popDamage;
    147.  
    148.             if (hitEffect)
    149.             {
    150.                 Instantiate(hitEffect, transform.position, transform.rotation);
    151.             }
    152.  
    153.             if (flinch)
    154.             {
    155.                 dir = (other.transform.position - transform.position).normalized;
    156.                 other.GetComponent<StatusC>().Flinch(dir);
    157.             }
    158.  
    159.             if (!penetrate)
    160.             {
    161.                 Destroy(gameObject);
    162.             }
    163.         }
    164.     }
    165.  
    166.     private IEnumerator TemporaryDisableHit(float duration)
    167.     {
    168.  
    169.         Collider objectsCollider = GetComponent<Collider>();
    170.  
    171.         objectsCollider.enabled = false;
    172.  
    173.         yield return new WaitForSeconds(duration);
    174.  
    175.         objectsCollider.enabled = true;
    176.     }
    177. }
    Simples :)
     
    keepthachange likes this.
  11. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Okay well actually that depends keepthachange. If you wanna disable the collider on the cube. Make a new script with a new 'OnTriggerEnter', put the script on the Cube, and then call the Coroutine like you've seen me do from in the OnTriggerEnter. If you want the player's Collider to turn off for that amount of time, add it to the player's script like I did above.

    So you'd just need to add this to a script new script and put it on the cube:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DisableCubeCollider : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private float disableDuration;
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         StartCoroutine(TemporaryDisableHit(disableDuration));
    12.     }
    13.  
    14.     private IEnumerator TemporaryDisableHit(float duration)
    15.     {
    16.         Collider objectsCollider = GetComponent<Collider>();
    17.  
    18.         objectsCollider.enabled = false;
    19.  
    20.         yield return new WaitForSeconds(duration);
    21.  
    22.         objectsCollider.enabled = true;
    23.     }
    24. }
     
  12. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    You don't made this script... so yes this is a problem :)
    I don't proposed WakeForSeconds() because if you change animation for longer/shorter you also must change "duration". If SetActive will be turn when animation is finished problem would not be.
     
  13. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    i dont want player collider to turn off i just want the cubs collider to turn off after the sript has delt its damage and what ever it needed to do on collide and i also do not want it to deal damage to player or ally. atm it does not do anything to player or ally just enemy but it just does not turn off on collide and then back on.. idk if im saying this right or if im just not getting it right from you guys lol.
     
  14. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    so what does this do, what you have added to script. cause i think this is what i might be looking for if im reading this right


    the Literally just like this: script lol
     
  15. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    difficult question: you will want ready script or do it yourself?
     
  16. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    That totally depends on which object's collider you want to turn off. Does the thing you want to turn off have a tag? Is it the 'Enemy' tag? You need to point out which object's collider you want to turn off.
     
  17. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    i want the cubes collider to turn( the cube i have my script on ) then back on after a moment or two

    so as soon as it hits the enemy(the enmeyis taged enemy) it turn off then turns back on after moment.
     
  18. Warsymphony

    Warsymphony

    Joined:
    Sep 23, 2014
    Posts:
    43
    I think it's best for each attack to have an ID and only call the appropriate methods if the next attack ID is different.
     
  19. Warsymphony

    Warsymphony

    Joined:
    Sep 23, 2014
    Posts:
    43
    attackID++ at the start of each attack
     
  20. rchmiel

    rchmiel

    Joined:
    Apr 5, 2015
    Posts:
    49
    keepthachange are you solved the problem?