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. Dismiss Notice

HELP:Melee raycast do after another one is finished

Discussion in 'Scripting' started by MODEEEEE, Jun 8, 2014.

  1. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
    So heres the deal i've been working on meele script,it works perfectly,damage lines up with animations but my Sound.Impact hole and prefab can appear a lot of times when you click mouse to fast,how to prevent that?
    Raycast at line 60-93

    Code (JavaScript):
    1. #pragma strict
    2. var TheDamage : int = 50;
    3. var Distance : float;
    4. var MaxDistance : float = 1.5;
    5. var TheAnimator : Animator;
    6. var DammageDelay : float = 0.6;
    7.  
    8. var Hit01Streak = 0;
    9. var Hit02Streak = 0;
    10. var Hit : AudioClip[];
    11. var bloodFX : GameObject;
    12. var decal : GameObject;
    13. var addpos : float = 0;
    14. var canhit : boolean = false;
    15.  
    16. function Update () {
    17.  
    18.  
    19.  
    20.  
    21. if(Input.GetButtonDown("Fire1") && canhit)
    22. {
    23. AttackDammage();
    24. }
    25. }
    26.  
    27. function AttackDammage() {
    28. if(Random.value >= 0.5 && Hit01Streak <=2)
    29. {
    30. TheAnimator.SetBool("Hit01", true);
    31. Hit01Streak += 1;
    32. Hit02Streak = 0;
    33.  
    34.  
    35.  
    36. }
    37. else
    38. {
    39. if (Hit02Streak <= 2)
    40.     {
    41.  
    42. TheAnimator.SetBool("Hit02",true);
    43. Hit01Streak = 0;
    44. Hit02Streak += 1;
    45.  
    46.  
    47.  
    48.     }  
    49.     else
    50.     {
    51.     TheAnimator.SetBool("Hit01", true);
    52.     Hit01Streak +=1;
    53.     Hit02Streak = 0;
    54.    
    55.  
    56.     }
    57. }
    58. yield WaitForSeconds(DammageDelay);
    59.  
    60. //actual attack
    61. var hit : RaycastHit;
    62. var ray = Camera.main.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, 0));
    63. if(Physics.Raycast(ray, hit))
    64. {
    65. Distance = hit.distance;
    66. if(Distance < MaxDistance)
    67. {
    68. //var hitRotation = Quaternion.Euler( Random.Range(0, 360) , 0 , 0); //not working
    69. var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    70.  
    71. Instantiate(bloodFX, hit.point, hitRotation);
    72. Instantiate(decal, hit.point + (hit.normal * addpos), hitRotation);
    73.  
    74.  
    75.  
    76.  
    77. hit.transform.SendMessage("ApplyDammage", TheDamage , SendMessageOptions.DontRequireReceiver);
    78.   // var blood = Instantiate(bloodFX, transform.position, Quaternion.identity);
    79. audio.clip = (Hit[Random.Range(0,Hit.length)]);
    80. audio.Play();  
    81.  
    82.  
    83.  
    84.  
    85.  
    86.  
    87.    
    88.    
    89. }
    90. }
    91. TheAnimator.SetBool("Hit01", false);
    92. TheAnimator.SetBool("Hit02", false);
    93. }
    94.  
     
  2. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
  3. Stellar

    Stellar

    Joined:
    Jul 20, 2013
    Posts:
    4
    you could add some global cooldown to your attacks. i.e. to wait 1 second between every attack:

    Code (JavaScript):
    1. var attackTimer : float = 0.0;
    2. var coolDown : float = 1.0;
    3.  
    4. function Update() {
    5.   if(attackTimer > 0)
    6.     attackTimer -= Time.deltaTime;
    7.      
    8.   if(attackTimer < 0)
    9.     attackTimer = 0;
    10.  
    11.   if(Input.GetButtonDown("Fire1") && canhit) {
    12.     if(attackTimer == 0) {
    13.       AttackDammage();
    14.       attackTimer = coolDown;
    15.     }
    16.   }
    17. }
    Not sure if you want the delay on the attack, or on the sound/bloodFX. If you want it on the sound and blood FX you could also place the if(attackTimer == 0) { ... attackTimer = coolDown; } around that part instead of around AttackDammage();


    Sorry if syntax is not right, i'm used to programming in c#

    Hope this helps!
     
    Last edited: Jun 9, 2014
  4. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
    It helped !!! Thank you!
     
  5. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
    Btw in my code line 68 i wanted to make random rotation in one axis,but it rotates in all axes,how can i solve it?
     
  6. Stellar

    Stellar

    Joined:
    Jul 20, 2013
    Posts:
    4
    Not sure what you're trying to do there. Are you trying to make the hit only apply in an arc in front of your character?
     
  7. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
    Im trying to make bullet hole 68 line rotation seems to be rotating in every axis while the one im using right(line 69) now works great but wont rotate at all
     
  8. Stellar

    Stellar

    Joined:
    Jul 20, 2013
    Posts:
    4
    I have no idea then. Maybe someone else can help. I'm not good with all the quaternion and angles stuff anyway hehe :D
     
  9. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
    Ok,np :)