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

Calling raycast function on animation dont work

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

  1. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
    So what im trying to do is when i press left mouse button animation plays and in middle of animation call function attack with will take some hp of another object but idk why but function is not working.. ;\

    Code (JavaScript):
    1. #pragma strict
    2. var TheDammage : int = 50;
    3. var Distance : float;
    4. var MaxDistance : float = 1.5;
    5. var TheSystem : Transform;
    6.  
    7. function Start () {
    8.  
    9. }
    10.  
    11. function Update () {
    12. if (Input.GetButtonDown("Fire1"))
    13.     {
    14.         //Attack animation - IF YOU ARE GETTING ERRORS BECAUSE YOU HAVENT MADE AN ANIMATION AND ATTACHED IT, DELETE THE FOLOWING LINE.
    15.         animation.Play("Swing");
    16.  
    17.     }
    18.     }
    19.     function attack() {
    20. var hit : RaycastHit;
    21.         if (Physics.Raycast(TheSystem.transform.position, TheSystem.transform.forward, hit, MaxDistance))
    22.         {
    23.            
    24.                 hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
    25.            
    26.         }
    27.         }
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var Health = 100;
    4.  
    5. function ApplyDammage (TheDammage : int)
    6. {
    7.     Health -= TheDammage;
    8.    
    9.     if(Health <= 0)
    10.     {
    11.         Dead();
    12.     }
    13. }
    14.  
    15. function Dead()
    16. {
    17.     Destroy (gameObject);
    18. }
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    TheSystem is already a transform, so you don need to access its transform. Also, you should use GetComponent and not SendMessage.

    Last remark: You have a function Attack, but you are not using it when the button is pressed.
     
  3. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    It seems that you're not calling the attack method in your code. Only specially named event method get called automatically by Unity. Any other methods have to be called explicitly.

    Try changing your Update method to this:
    Code (javascript):
    1. function Update()
    2. {
    3.     if (Input.GetButtonDown("Fire1"))
    4.     {
    5.         animation.Play("Swing");
    6.         attack();
    7.     }
    8. }
    9.  
     
  4. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
    I call that function in middle of animation,add animation event and so on.
     
  5. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52
    hmm cant i call function in animation event? like in middle of animation?
     
  6. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Show the relevant code that actually calls that function.
     
  7. MODEEEEE

    MODEEEEE

    Joined:
    Feb 15, 2014
    Posts:
    52