• 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 OnePlusOneNS · Dec 13, 2013 at 04:55 PM · animationmovementverticalhorizontal

Play animation for horizontal and vertical movement?

Hey,

i did some research but did not find a clear answer, and when I had something I hard problems applying it to my script. What I want to do is play an animation when my character (first person) is moving forward and backward and a different one when he is moving left and right. So basically is there a way I can check for horizontal movement and vertical movement and then apply a specific animation to that movement.

If it helps here is my code for the movement of the character.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(CharacterController))]
 public class FirstPersonController : MonoBehaviour {
     
     public float movementSpeed = 5.0f;
     public float mouseSensitivity = 2.0f;
     public float upDownRange = 60.0f;
     public float jumpSpeed = 3.0f;
     
     bool sprintCoolDown = false;
     float desiredUpDown = 90;    
     float verticalVelocity = 0;
     
     CharacterController characterController;
     
     // Use this for initialization
     void Start () {
         
         Screen.lockCursor = true;
         characterController = GetComponent<CharacterController>();
         
     }
     
     // Update is called once per frame
     void Update () {        
         // Rotation
         
         float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
         transform.Rotate(0 , rotLeftRight , 0);
         
         desiredUpDown -= Input.GetAxis("Mouse Y") * mouseSensitivity;
         desiredUpDown = Mathf.Clamp(desiredUpDown, -upDownRange , upDownRange);
         Camera.main.transform.localRotation = Quaternion.Euler (desiredUpDown , 0 , 0 );
         
         // Movement of Character
         
         if(Input.GetKey(KeyCode.LeftShift) && sprintCoolDown == false) {
             movementSpeed = 8.0f;
             StartCoroutine(sprintCD());
         }
         else {
             movementSpeed = 5.0f;    
         }
         float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
         float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
         
         verticalVelocity += Physics.gravity.y * Time.deltaTime;
         
         if( characterController.isGrounded && Input.GetButtonDown("Jump") ) {
             verticalVelocity = jumpSpeed;    
         }
         
         Vector3 speed = new Vector3 ( sideSpeed , verticalVelocity , forwardSpeed );
         
         speed = transform.rotation * speed;
                         
         characterController.Move( speed * Time.deltaTime );
             
     }
     IEnumerator sprintCD() {
             yield return new WaitForSeconds(6);
             sprintCoolDown = true;
             yield return new WaitForSeconds(4);
             sprintCoolDown = false;        
         }
 }

Any help is appreciated. Thanks a lot. Kind Regards Cribbel (Still a fair noob at unity but getting the hang of it)

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

1 Reply

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

Answer by LukaKotar · Dec 13, 2013 at 05:20 PM

By default, Unity has input controls set up for 'Horizontal' and 'Vertical'. You can see them by going into Edit>Project Settings>Input. There will be four fields: 'Positive Button', 'Negative Button', and the 'Alt Positive Button' and 'Alt Negative Button'.

So, vertical input would be W or S, or the up and down arrow keys. W and arrow up are positive, so they will return 1, while S and arrow down, will return -1. If none of those is pressed, it will return 0. The same is for the horizontal input.

Here is how you would play different animations according to which buttons are pressed:

 void Update(){
     //If the player is moving horizontally (left and right), and not diagonally or vertically
     //Remember: 1 is right (positive), and -1 is left (negative).
     if(Input.GetAxis("Horizontal") != 0 && Input.GetAxis("Vertical") == 0) {
         //Play your sideways animation
         animation.Play("Insert animation name here");
     }
     //If the player is moving vertically (forward and backward) or diagonally
     else {
         //Play your forward/backward animation
         animation.Play("Insert animation name here");
     }
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can I play different animations depending on the direction my character is moving? 1 Answer

Stop Movement while attacking 2 Answers

Adapting Horizontal and Vertical movement to New Input System? 0 Answers

Make movement vertically,How to move Vertically? 1 Answer


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