- Home /
How can i start a 2D animation via script
I can't quite get my head around how 2D animation works, i imported my sprite sheet for the "freeze" effect but i don't want that as the default animation, so i created an idle animation as well.
Here is what i want. i want to control my character as normal, but if i stop moving (no user input) i want my character to start freezing. This should be simple enough with just adding an else after my If input section of my move script, but i still don't know how to actually play the freeze animation.
Here is my script if you're finding it hard to understand.
using UnityEngine;
public class move : MonoBehaviour { [HideInInspector] public bool facingRight = true; // For determining which way the player is currently facing. [HideInInspector]
 public float moveForce = 365f;            // Amount of force added to move the player left and right.
 public float maxSpeed = 5f;                // The fastest the player can travel in the x axis.
 public float jumpForce = 1000f;            // How high the player will jump
 public float downForce = -1000f;        // the ammount of force the player uses to crash down to the ground
 public bool canbreak = false;            // can this model break into clones?
 public Rigidbody2D projectile;            // Defining a rigidbody (physics object) in game (this is where we choose the clones models in unity and attach to the script)
 public int maxClones = 5;                // Setting the max ammount of clones
 public bool canShrink = true;
 public bool canGrow = false;
 public Animator anim;
 
 void Awake()
 {
     anim = GetComponent<Animator>();
 }
 void FixedUpdate ()  //Fixed update means this function will Check/run whatever is inside this EVERY frame.
 {
     float h = Input.GetAxis("Horizontal");    //setting a horizontal variable for movement
     if(h * rigidbody2D.velocity.x < maxSpeed) 
         rigidbody2D.AddForce(Vector2.right * h * moveForce); // Adding our movement velocity to our player
     
     if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
         rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
     
     if(h > 0 && !facingRight)         //checking if the player is not facing right
         Flip();                        //flipping the 2d image when we turn left or right
     else if(h < 0 && facingRight)    //same
         Flip();                        //same
     
     if(Input.GetKeyDown(KeyCode.Space))        //if we press spacebar
     {
         rigidbody2D.AddForce(new Vector2(0f, jumpForce));        //add force upwards (jump) and * the ammount by our jumpForce Variable
     }
     if(Input.GetKey(KeyCode.S))            //if we press S
     {
         rigidbody2D.AddForce(new Vector2(0f, downForce));    //add a downwards force
         canbreak = true;    //can this player break up into clones?
     }
     if(Input.GetKeyDown(KeyCode.W))     //If we press W
     {
         canShrink = false;//Grow function
     }
     else
     {
         //I WANT THE FREEZE ANIMATION TO BEGIN HERE.
     }
 }
 
 
 void Flip ()
 {
     facingRight = !facingRight;        // Switch the way the player is labelled as facing.
     
     Vector3 theScale = transform.localScale;    // Multiply the player's x local scale by -1.
     theScale.x *= -1;
     transform.localScale = theScale;
 }
}
Answer by mrlovelyface · Jan 20, 2014 at 06:03 PM
Buddy I am not sure if this will help, it helped me out :)
http://www.raywenderlich.com/61532/unity-2d-tutorial-getting-started
Your answer
 
 
             Follow this Question
Related Questions
Simultaneous Animation for an Object 0 Answers
I cannot make AnimationClip changing some Sprites without AnimationController. 0 Answers
Sprites: Project Sprite to Mesh, allow for bending of sprites for 3d environments 0 Answers
Object not moving after changing animator parameter 1 Answer
Sprite Rect Moves Between Frames 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                