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! Accessing Method&Animation on another GameObject

Discussion in 'Scripting' started by steb92, Mar 13, 2015.

  1. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    Hello all,

    I'm trying to play a weapon attack animation (created using Unity's animation tool) when the player presses the "attack" button (in this case, B on an Xbox controller) but it doesn't seem to want to play.

    I'm using two scripts. The first is my character controller, which currently contains the following.
    Code (CSharp):
    1.        if (OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.B))
    2.        {
    3.            print("B ATTACK");
    4.            weapon = GameObject.Find("fireaxe");
    5.            AttackAnimation attack = (AttackAnimation)weapon.GetComponent(typeof(AttackAnimation));
    6.            attack.Attack();
    7.        
    8.         }
    I'm trying to access my second script, which is named AttackAnimation, and is located on the fireaxe GameObject. The fireaxe GameObject is a child of the Player_VR, which contains the above script.



    The AttackAnimation.cs looks like this;
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AttackAnimation : MonoBehaviour {
    5.  
    6.     public AnimationClip AttackAnimationClip;
    7.  
    8.     public void Attack()
    9.     {
    10.         animation.Play(AttackAnimationClip.name);
    11.     }
    12.  
    13.     public void Update()
    14.     {
    15.         if (IsAttackFinished)
    16.         {
    17.             animation.CrossFade(animation.clip.name);
    18.         }
    19.     }
    20.  
    21.  
    22.     public bool IsAttackFinished    {
    23.         get {
    24.             return animation[AttackAnimationClip.name].time > animation[AttackAnimationClip.name].length; }
    25.             }
    26. }
    27.  
    Any help is greatly appreciated!

    P.s. Currently, when the player presses 'B' on the gamepad, the system prints out "B ATTACK" to the console. This was just me testing I had the gamepad configured correctly.
     
  2. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    If you print something inside the Attack method of AttackAnimation.cs, do you see it?
     
  3. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
  4. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    I can't seem to find any error in your script.
    Can you take a screenshot of your gameObject with the animations set?
     
  5. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
  6. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Ok.
    Let's try some things.

    In the Animation gameComponent there's an array of animations you can assign, being the one defined in the Animation field to be the default one. Do you have the fireaxe_attack animation inside the Animations Array?

    Also, I believe you could do this instead, since you already assign the animation in your script.
    Code (CSharp):
    1. using UnityEngine;
    2.     public AnimationClip AttackAnimationClip;
    3.  
    4.     public void Attack()
    5.     {
    6.         AttackAnimationClip.Play();
    7.     }
    8. }
    At last, the API says that animation.Play return a false value if failed. Why don't you print the result from var a = animation.Play(blabla); print(a); to check the returned value.
     
  7. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    I do not.

    error.png
     
  8. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Ok. The second one can't compile.

    What about inserting your fireaxe_attack inside the array of the animation component? Or the responde from Animation.Play("fireaxe_attack")?
     
  9. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    Ok, so I added fireaxe_attack to the Animation array and now I'm using

    Code (CSharp):
    1.        if (OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.B))
    2.        {
    3.  
    4.            weapon = GameObject.Find("fireaxe");
    5.            //AttackAnimation attack = (AttackAnimation)weapon.GetComponent(typeof(AttackAnimation));
    6.            //attack.Attack();
    7.            weapon.animation.Play("fireaxe_attack");
    8.          
    9.         }
    And it's working, but the animation is in a loop once started, despite being set to 'Ping pong' in the legacy settings.
     
  10. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    It loop from start to end instead of doing start end start?

    Last time I tried, I didn't manage to config that ping pong thing. I change the animation to do it by itself :p
     
  11. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    Thanks for the help. My combat system now works again!

    However, a slight bug means that, when I press B to attack, the axe swings (i.e. the animation plays) but, if I press B a second time (i.e. to attack again) nothing happens.