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

Switching between 2 Animation constantly Please Help! C# Script

Discussion in 'Scripting' started by ITheSpartaI, Apr 14, 2014.

  1. ITheSpartaI

    ITheSpartaI

    Joined:
    Jan 4, 2014
    Posts:
    12
    Hello Guys,
    For my Game i need an script that is playing 2 Animation alternately.
    So lets say i have a Cube that reaches one side of my Map with animation 1 i want that it moves back with animation 2.
    Maybe someone can help me because i cant "write" good C# Scripts
    I only need C# Scripts please. Thanks for your Help
     
  2. Gustav_Bok

    Gustav_Bok

    Joined:
    Dec 6, 2012
    Posts:
    35
    Hi,
    Here is an easy take to it.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AnimationChanger : MonoBehaviour {
    6.  
    7.     private Animation currentlyPlaying;
    8.  
    9.     public Animation animationOne;
    10.     public Animation animationTwo;
    11.    
    12.     void Start () {
    13.         currentlyPlaying = animationOne;
    14.     }
    15.  
    16.     void Update () {
    17.  
    18.         if(!currentlyPlaying.isPlaying  currentlyPlaying == animationOne)
    19.         {
    20.             animationTwo.Play();
    21.             currentlyPlaying = animationTwo;
    22.         }
    23.         else if(!currentlyPlaying.isPlaying  currentlyPlaying == animationTwo)
    24.         {
    25.             animationOne.Play();
    26.             currentlyPlaying = animationOne;
    27.         }
    28.     }
    29. }
    30.