Search Unity

Animation direction...help please..

Discussion in '2D' started by Bydark, Jan 31, 2018.

  1. Bydark

    Bydark

    Joined:
    Oct 9, 2013
    Posts:
    10
    Hi, I'm having trouble doing the running animation.

    The problem appears when I want to change direction while the character is running, it seems that being so fast, the character slides on the floor.

    I have two videos, in one the speed is low and in another it is high.

    VIDEO1: https://gyazo.com/1dd3dd4ba68fe6eceffd2b1ced6b9284

    VIDEO2: https://gyazo.com/0b4c855937572048ce4d4e3f6dcbdc5b

    Here I leave the code that I am using.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.  
    8.     public float speed = 2f;
    9.     private Rigidbody2D rb2d;
    10.     public float maxSpeed = 5f;
    11.     private Animator anim;
    12.     public bool grounded;
    13.     public bool pressrun;
    14.  
    15.  
    16.     void Start () {
    17.  
    18.  
    19.         rb2d = GetComponent<Rigidbody2D> ();
    20.         anim = GetComponent<Animator> ();
    21.        
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.  
    28.         anim.SetFloat ("Speed", Mathf.Abs(rb2d.velocity.x));
    29.         anim.SetBool ("Grounded", grounded);
    30.         anim.SetBool ("PressRun", pressrun);
    31.  
    32.     }
    33.  
    34.  
    35.  
    36.  
    37.     void FixedUpdate(){
    38.    
    39.    
    40.    
    41.         float h = Input.GetAxis ("Horizontal");
    42.         rb2d.AddForce (Vector2.right * speed * h);
    43.  
    44.  
    45.  
    46.        
    47.         float limitedSpeed = Mathf.Clamp (rb2d.velocity.x, -maxSpeed, maxSpeed);
    48.         rb2d.velocity = new Vector2 (limitedSpeed, rb2d.velocity.y);
    49.  
    50.  
    51.         if (Input.GetKey ("k")) {
    52.  
    53.             pressrun = true;
    54.             rb2d.velocity = new Vector2 (limitedSpeed * 2, rb2d.velocity.y);
    55.  
    56.  
    57.         } else {
    58.  
    59.  
    60.             pressrun = false;
    61.  
    62.         }
    63.  
    64.         if (h > 0.1f) {
    65.        
    66.             transform.localScale = new Vector3 (1f, 1f, 1f);
    67.        
    68.         }
    69.  
    70.  
    71.         if (h < -0.1f) {
    72.  
    73.             transform.localScale = new Vector3 (-1f, 1f, 1f);
    74.  
    75.         }
    76.  
    77.     }
    78.  
    79.  
    80.  
    81. }
    82.  
    Thank you very much and I am sorry for my English.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could keep track of the direction they're travelling and if it changes, set the x portion of the velocity to zero before you apply the new force?