Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by John_Freeman · Mar 24, 2016 at 03:34 AM · animation2dscripting problem

Can't seem to get my animations to work proper

So I'm working on a game that has the player movement in the style of a 2D RPG and I have walking animations setup for when the player is facing in each direction (up, down, left, right). I wrote my script and it make the player face in those directions but I want to make it so when you try to go diagonally it just does the animation for walking on the horizontal axis. So for example holding the up and left keys makes the player walk in the north-west direction but I want just the left walking animation to play rather than it rapidly switch between the up and left animations. I'm really stumped on how I should go about writing the code for this so it works how I want. Here is the script that I wrote for my character movement and playing animations:

 using UnityEngine;
 using System.Collections;
 
 public class player_movement : MonoBehaviour {
 
     [SerializeField]
     private float speed;
 
     private Animator playerAnimator;
     private Rigidbody2D playerRigidbody;
 
     void Start()
     {
         playerRigidbody = GetComponent<Rigidbody2D>();
         playerAnimator = GetComponent<Animator>();
     }
 
     void FixedUpdate()
     {
         float horizontal = Input.GetAxisRaw("Horizontal");
         float vertical = Input.GetAxisRaw("Vertical");
 
         HandleMovement(horizontal, vertical);
         Facing(vertical, horizontal);
     }
     
     void HandleMovement(float horizontal, float vertical)
     {
         playerRigidbody.velocity = new Vector2(horizontal * speed, vertical * speed);
     }
 
     private void Facing(float vertical, float horizontal)
     {
         if (vertical > 0.1){
             playerAnimator.SetFloat("up", 1);
         }
         else{
             playerAnimator.SetFloat("up", 0);
         }
         
         if (vertical < -0.1){
             playerAnimator.SetFloat("down", 1);
         }
         else{
             playerAnimator.SetFloat("down", 0);
         }
 
         if (horizontal > 0.1)
         {
             playerAnimator.SetFloat("right", 1);
         }
         else {
             playerAnimator.SetFloat("right", 0);
         }
 
         if (horizontal < -0.1)
         {
             playerAnimator.SetFloat("left", 1);
         }
         else {
             playerAnimator.SetFloat("left", 0);
         }
 
     }
 }
 

If someone could tell me how I should be going about this I would really appreciate it. I'm just getting started with scripting and I'm still really new.

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

Answer by Abhiroop-Tandon · Mar 24, 2016 at 10:42 AM

try this instead and let me know if it works or not

      private void Facing(float vertical, float horizontal)
      {
  
          if (horizontal > 0.1)
          {
              playerAnimator.SetFloat("right", 1);
              playerAnimator.SetFloat("left", 0);
          }
  
         else if (horizontal < -0.1)
          {
              playerAnimator.SetFloat("left", 1);
              playerAnimator.SetFloat("right", 0);
          }
 
          else{
                 if (vertical > 0.1){
                       playerAnimator.SetFloat("up", 1);
                       playerAnimator.SetFloat("down", 0);
                 }
          
                 if (vertical < -0.1){
                       playerAnimator.SetFloat("down", 1);
                       playerAnimator.SetFloat("up", 0);
                 }
           }
      }


Comment
Add comment · Show 3 · 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 John_Freeman · Mar 25, 2016 at 08:15 AM 0
Share

I had high hopes but this doesn't work. Sorta just makes it rapidly switch between 2 animations after pressing any key combos or pressing the right key.

avatar image Abhiroop-Tandon John_Freeman · Mar 25, 2016 at 09:29 AM 0
Share

So whats exactly happening now ??

avatar image John_Freeman Abhiroop-Tandon · Mar 25, 2016 at 11:17 PM 0
Share

Basically if I press any two keys to move horizontally it just quickly switches between the facing animation for both directions. Other than that trying your code also made it so that some times just pressing the left key makes it do what I described in the last sentence.

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

101 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i stop my shooting animation after playing? 0 Answers

How to immediately CHANGE SPEED from spotted position to another?) 1 Answer

2D Spine Animation not playing 0 Answers

Player should turn in the direction the player is running. (2D Game) 0 Answers

HELP,Watched tutorial https://www.youtube.com/watch?v=Xnyb2f6Qqzg 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