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

Animation issue with animation script on seperate game object.

Discussion in 'Animation' started by steb92, Mar 9, 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.
     

    Attached Files:

  2. steb92

    steb92

    Joined:
    Nov 7, 2014
    Posts:
    29
    Anyone at all able to suggest a fix for this? I'm trying to access a method on a separate gameobject that plays a specific animation on that gameobject.