Help me to switch animation from running to falling

I have 2 fbx files

  1. running_40.fbx , see the image

A script is attached to the Soldier GO, here is the script

using UnityEngine;
using System.Collections;
 
public  class characterScript : MonoBehaviour
{
        //references
	    characterScript charScript;
	
        public static float SwitchingSpeed=8.0f;
        public static Vector3 pos;
       
        public float ForwardSpeed;
        public int HowFarToGo;
        private bool OnLeft;
        private bool OnRight;
        public static bool IsSwitching;
        private int counter;
        public static bool ALTERNATOR, LeftOrRight;
       
       
        public static CharacterController controller;
        private string activeAnimation;
       
       
        // move variables
        public float runSpeed = 8f;
       public static float hero_z_position;
	public static float hero_x_position;
       
        private int leftyes=1;
       
       
        void Start ()
        {
                ALTERNATOR = true;
                OnRight = true;
                controller = gameObject.GetComponent<CharacterController>();
		//Debug.Log(leftyes);
        }
       
          void OnEnable()
        {
                        FingerGestures.OnFingerSwipe += FingerGestures_OnFingerSwipe;
        }
 
        void OnDisable()
        {
                        FingerGestures.OnFingerSwipe -= FingerGestures_OnFingerSwipe;
        }
       
       
        void FingerGestures_OnFingerSwipe( int fingerIndex, Vector2 startPos, FingerGestures.SwipeDirection direction, float velocity )
    {
                if(direction==FingerGestures.SwipeDirection.Left  leftyes==0){
                        leftyes=1;
                        Debug.Log("its left and leftyes=" +leftyes);
                }
               
                if(direction==FingerGestures.SwipeDirection.Right  leftyes==1){
                        leftyes=0;
                        Debug.Log("its Right and leftyes=" +leftyes);
                }
        }
       
       
        void Update ()
        {
		
		
                controller.Move(transform.forward * 8f * Time.deltaTime);
		hero_z_position = transform.position.z;
		hero_x_position = transform.position.x;
		//Debug.Log(hero_x_position);
		
		 if(Input.GetKeyDown(KeyCode.X)){
			Debug.Log("pressed x");
		}
                //horizontal
                if (leftyes==1)
                {
                        pos = transform.right * (-SwitchingSpeed) * Time.deltaTime;
                        pos.x = Mathf.Clamp(pos.x,-5.5f,5.5f);
                        controller.Move(pos);
                }
                else if (leftyes==0)
                {
                        pos = transform.right * SwitchingSpeed * Time.deltaTime;
                        pos.x = Mathf.Clamp(pos.x,-5.5f,5.5f);
                        controller.Move(pos);  
                }
		float newX = Mathf.Clamp(transform.position.x,-5.5f,0.5f);
        transform.position = new Vector3(newX, transform.position.y, transform.position.z);
        }  
}

I want to play animation when button x is pressed[refer above code], which is in other fbx called SmartGuy@smart_guy_tripping.fbx

How can i do it ?

don’t you have to do something like this ? :

and don’t you have to set your animations to loop or playonce sort of thing in the inspector ?

Jessica for this you need to follow these steps:

  • First select model Go in Inspector Select Rig>Animation Type>Legacy>Apply.
    *Select Animation divide the animation in parts as your requirement. Suppose your animation contains 0 to 50 frames. First 0 to 25 frame for running 25 to 50 frame for Falling. Animation name should be same in script as your write here in model.

  • After that Create a script attach that with model.

using UnityEngine;

using System.Collections;

public class BowAnimation : MonoBehaviour {

	private Animation BowAnim;
	

	bool anim_play;
	
	// Use this for initialization
	void Start () {
	
		BowAnim=transform.animation;
		
		BowAnim["Running"].wrapMode=WrapMode.Once;
		
		BowAnim["Falling"].wrapMode=WrapMode.Once;
		
		BowAnim.Stop();
	}
	
	
	public void StartAnim()
	{
		
		BowAnim.Play("Running");
	}
	
	public void FrontAnim()
	{
		
		BowAnim.Play("Falling");
	}

}

i have an fbx with has a running animation

and a different fbx which has a falling animation. I dont have 1 animation which contains all different animations frame wise

No Problem.

Then you need to follow just first step for each model.

Add different-2 script with both. Just add these script.

using UnityEngine;

 

using System.Collections;

 

public class BowAnimation : MonoBehaviour {

 

    private Animation BowAnim;

    

 
    

    // Use this for initialization

    void Start () {

    

        BowAnim=transform.animation;
        
         BowAnim.Stop();

    }

    

    

    public void StartAnim()

    {

        

        BowAnim.Play();

    }

    

  
 

}

After adding doing this just call these function from your script.

i am close but still lil confused.

  1. DO i need to make each script for an fbx and join it to its fbx ?
  2. and then call the functions from these 2 script into my main script ??

when you drag the fbx model from Project window to Hierarchy window It becomes an Prefab. Then you can attach the script with both model.
Then call these function from your main script.