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 Condore · Sep 07, 2012 at 08:35 PM · animationaxisjoystickdiagonal

Problem with diagonal movement + animations

Hey guys,

I'm starting to animate my character for my school project and I have a small problem, I have my animator, handling all of our characters animations based on how far you push the left thumbstick and I've got the animations crossfading between each other from "walk to "run" the only problem is, when I push the thumbstick diagonal so that it's in between the x and z axis, it puts my character back to walk, instead of run, even if I have the thumbstick pushed all the way.

how exactly can I fix this?

 using UnityEngine;
 using System.Collections;
 
 public class TP_Animator : MonoBehaviour
 {
     public enum Direction
     {
         Forward, RightBackward, RightForward, Backward, LeftBackward, LeftForward, Right, Left, Stationary    
     }
     
     public enum CharacterState
     {
         Idle, Walk, Run, Strafe, StartCombo, Combo1, Combo2
     }
     
     public ThirdPersonController Controller;
     
     public static TP_Animator Instance;
     
     public Direction MoveDirection { get; set;}
     public CharacterState State {get; set;}
     
     void Awake()
     {
         Instance = this;
         
         //Attack animations are on different layer's for prioirity within the combo.
         animation["attack1"].layer = 1;
         animation["attack2"].layer = 2;
         animation["attack3"].layer = 3;
     }
     
     void Start()
     {
     
     }
     
     void Update()
     {
         DetermineCurrentMoveDirection();
         DetermineCurrentState();
         ProcessCurrentState();
     }
     
     public void DetermineCurrentMoveDirection()// this functions determines which direction the character is moving an plays animation 
     {
         Debug.Log("Movement is occuring!!" + Controller.movement1);
         
         if (Controller.movement1.x > 0)
         {
             // moving Right    
             if (Controller.movement1.z > 0f)
             {
                 MoveDirection = Direction.RightForward;
             }
             else if (Controller.movement1.z < 0f)
             {
                 MoveDirection = Direction.RightBackward;
             }
             else
             {
                 MoveDirection = Direction.Right;
             }
             
         }
         else if(Controller.movement1.x < 0)
         {
                 //Move Left
                 if(Controller.movement1.z > -0)
                 {
                     MoveDirection = Direction.LeftBackward;
                 }
                 else if(Controller.movement1.z < -0)
                 {
                     MoveDirection = Direction.LeftForward;    
                 }
                 else
                 {
                     MoveDirection = Direction.Left;    
                 }
         }
         else
         {
             if(Controller.movement1.z > 0)
             {
                 MoveDirection = Direction.Forward;
             }
             else if(Controller.movement1.z < 0)
             {
                 MoveDirection = Direction.Backward;    
             }
             else
             MoveDirection = Direction.Stationary;    
         }
     }
     
     void DetermineCurrentState()// State tree, Checking to see if we're doing anything other than moving
     {
             switch (MoveDirection)
             {
                 case Direction.Stationary:
                     State = CharacterState.Idle;
                     break;
                 
                 case Direction.Forward:
                     if(Controller.movement1.z * Controller.speed < Controller.speed * 0.5f)
                     State = CharacterState.Walk;
                     else
                     State = CharacterState.Run;
                     break;
             
                 case Direction.RightForward:
                     if(Controller.movement1.z * Controller.speed < Controller.speed * 0.5f && JoyMag())
                     State = CharacterState.Walk;
                     else
                     State = CharacterState.Run;
                     break;
             
                 case Direction.LeftForward:
                     if(-Controller.movement1.x * Controller.speed < Controller.speed * 0.5f && JoyMag())
                     State = CharacterState.Walk;
                     else
                     State = CharacterState.Run;                    
                     break;
                 
                 case Direction.Left:
                     if(-Controller.movement1.x * Controller.speed < Controller.speed * 0.5f)
                     State = CharacterState.Walk;
                     else
                     State = CharacterState.Run;
                     break;
                 
                 case Direction.Right:
                     if(Controller.movement1.x * Controller.speed < Controller.speed * 0.5f)
                     State = CharacterState.Walk;
                     else
                     State = CharacterState.Run;                    
                     break;
                 
                 case Direction.Backward:
                     if(-Controller.movement1.z * Controller.speed < Controller.speed * 0.5f)
                     State = CharacterState.Walk;
                     else
                     State = CharacterState.Run;                    
                     break;
                 
                 case Direction.LeftBackward:
                     if(-Controller.movement1.x * Controller.speed < Controller.speed * 0.5f && JoyMag())
                     State = CharacterState.Walk;
                     else
                     State = CharacterState.Run;                    
                     break;
                 
                 case Direction.RightBackward:
                     if(-Controller.movement1.z * Controller.speed < Controller.speed * 0.5f)
                     State = CharacterState.Walk;
                     else
                     State = CharacterState.Run;                    
                     break;
             }
     }
     
     #region Character State Methods
     public void Idle()
     {
         
         
             State = CharacterState.Idle;
             animation.CrossFade("idle");
         
     }
     public void Walk()
     {
         
         
             State = CharacterState.Walk;
             animation.CrossFade("walk");
         
     }
         public void Run()
     {
         //if(!animation.isPlaying)
         //{
             State = CharacterState.Run;
             animation.CrossFade("run");
         //}
     }
     
     void StartCombo()
     {
         //if (!animation.isPlaying)
         {
           State = CharacterState.StartCombo;
           animation.CrossFade("attack1");
         }
     }
     
     void Combo1()
     {
         //if (!animation.isPlaying)
         {
             State = CharacterState.Combo1;
             animation.CrossFade("attack2");    
         }
     }
     
     void Combo2()
     {
         //if (!animation.isPlaying)
         {
             State = CharacterState.Combo2;
             animation.CrossFade("attack3");    
         }
     }
     #endregion
     
     #region Start Action Methods
     public void Attack()
     {    
         State = CharacterState.StartCombo;
         animation.CrossFadeQueued("attack1", 0.2f, QueueMode.PlayNow);
     }
     
     public void Attack1()
     {        
         State = CharacterState.Combo1;
         animation.CrossFadeQueued("attack2", 0.1f, QueueMode.CompleteOthers);
     }
     
     public void Attack2()
     {    
         State = CharacterState.Combo2;
         animation.CrossFadeQueued("attack3", 0.1f, QueueMode.CompleteOthers);
     }
         
     #endregion
     public void ProcessCurrentState() //Constantly Porcessing the current state of the character
     {
         switch (State)
         {
             case CharacterState.Idle:
                 Idle();
                     break;
             
         case CharacterState.Walk:
                 Walk();
                     break;
             
             case CharacterState.Run:
                 Run();
                     break;
             
             case CharacterState.StartCombo:
                 StartCombo();
                     break;
             
             case CharacterState.Combo1:
                 Combo1();
                     break;
             
             case CharacterState.Combo2:
                 Combo2();
                     break;
             
         }
     
         
         
     }
     
         public void JoyMag()
         {
             Vector2.SqrMagnitude(new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")));
         }
 }
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
1
Best Answer

Answer by ThermalFusion · Sep 07, 2012 at 09:40 PM

You're going in the right direction with the JoyMag function at the bottom, instead of switching between run and walk when only the x axis or z axis is below the threshold, do it with the magnitude of a Vector2 of the speed in x and z.

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 Condore · Sep 07, 2012 at 10:47 PM 0
Share

I thought I was going the right way, could you possibly include some pseudo code? I'd appreciate it.

I kind of understand what you're saying.

avatar image ThermalFusion · Sep 08, 2012 at 09:03 PM 0
Share
 Calculate movement magnitude
 if movement magnitude > run threshold
     play run
 else if movement magnitude > 0
     play walk
 else
     play idle

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

9 People are following this question.

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

Related Questions

Pay animation if joystick pushed to left and another if joystick is pushed to right 2 Answers

Joystick Diagonal? 2 Answers

Assigning joystick axis 2 in editor 1 Answer

How can I use a joystick for a mobile app to control specific limbs of a character? 0 Answers

Animation Problem 2 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