Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Divinitize · Jan 13, 2014 at 07:51 PM · animation2dsprite

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;
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Divinitize · Jan 14, 2014 at 05:30 PM 0
Share

Still desperate for help

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges