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 923penguin · Jul 02, 2015 at 11:44 AM · animationbeginnerplatformer

[Answered] How can you set an animation to run based on certain functions or key presses?

I'm currently trying to create a 2D platformer game as a first project, but I'm having some trouble with the animating. I have a sprite created, and I have the actual animation; when I press Play on the Unity console, the player starts the running animation, and I can move around just fine. However, I would like to set it up so that on start, the player doesn't move, and will only animate when walking left or right. I will include my script so far, but if anyone needs to know any other information, feel free to ask. I will willingly provide screenshots, data, files, etc. to help.

 using UnityEngine;
 using System.Collections;
 
 public class NewMovement : MonoBehaviour {
 
     public Rigidbody2D rb;
     public float moveSpeed;
     public float jumpPower;
 
     private bool faceLeft = false;
     private bool moving = false;
 
     void Start () {
         gameObject.GetComponent<Rigidbody2D> ();
         faceLeft = false;
                   //I'm fairly certain that a bunch of the code has to be put in Start, so info is retrieved from the get-go.
     }
 
     void Movement() 
     { 
 
         Vector2 v =  GetComponent<Rigidbody2D>().velocity;
         if (Input.GetKey ("d")) 
         {
             v.x = moveSpeed;
             GetComponent<Rigidbody2D>().velocity = v;
             moving = true;
         }
 //Boolean values should be based off of the keys D and A; the sprite should only animate when one of those keys are pressed.
         else if (Input.GetKey ("a")) 
         {
             v.x = moveSpeed * -1.0f;
             GetComponent<Rigidbody2D>().velocity = v;
             moving = true;
         }
 
         else {
             v.x = 0.0f;
             GetComponent<Rigidbody2D>().velocity = v;
         }
 
         if (gameObject.transform.position.y <= -4.5) {
             if (Input.GetKeyDown ("w")) {
                 v.y = jumpPower;
                 GetComponent<Rigidbody2D> ().velocity = v;
             }
         }
     }
 
     void SpriteFlip ()
     {
 
         Vector2 face = GetComponent<Transform>().transform.localScale;
 
         if (Input.GetKeyDown ("a")) {
             if (!faceLeft){
 
                 face.x *= -1;
                 faceLeft = true;
                 GetComponent<Transform>().transform.localScale = face;
             }
         }
 
         if (Input.GetKeyDown ("d")) {
 
             if (faceLeft){
 
                 face.x *= -1;
                 faceLeft = false;
                 GetComponent<Transform>().transform.localScale = face;
             }
         }
     }
 
     void Animate()
     {
             //This is where the code will go for the animating, I hope.
     }
 
 
     void Update () 
     {
         Movement ();
         SpriteFlip ();
         Animate ();
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Ibzy · Jul 02, 2015 at 11:54 AM

It looks to me like the Learn tutorials are pretty helpful right here:

http://unity3d.com/learn/tutorials/projects/2d-roguelike/player-animator

This one shows how you set variables for triggering animations in the Animator component. Further tutorials int he series will cover how to trigger these from input.

Comment
Add comment · Show 2 · 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
avatar image 923penguin · Jul 02, 2015 at 01:51 PM 0
Share

Thank you for the link. I will look into it, and get back to you whether it helped or not.

avatar image 923penguin · Jul 02, 2015 at 08:50 PM 1
Share

The results are in: I watched the videos, and now I know how to do the animating! Thank you so much for helping me on this.

avatar image
0

Answer by komodor · Jul 02, 2015 at 11:46 AM

my approach would be using Animator component and just playing animations by animator.Play("aniatorStateName")

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Begginers question, Simplifing a script, opening two doors with 2 overlapping triggers. 1 Answer

How do you apply animations to characters with FPS input controls? 1 Answer

How to animate more platforms without using new animation for every single one? 0 Answers

How do I know when an object is going under a Platform Effector 2D object? 0 Answers

Can the animation editor create local rotational data? 3 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