Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help Needed: Character stuck facing left when idle

Discussion in 'Scripting' started by mirkytea_unity, Oct 5, 2020.

  1. mirkytea_unity

    mirkytea_unity

    Joined:
    Oct 5, 2020
    Posts:
    1
    Hello, I'm extremely new to scripting/coding on Unity so excuse the dumb question. I've been trying to implement code that lets my 2d sprite change direction when moving. though the code also inverts the sprites x-axis when idle and i can seem to fix it. below is a copy of my PlayerPlatformerController script. if there are any other mistakes/optimizations id very much appreciate the advice :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerPlatformerController : PhysicsObject {
    6.  
    7.     public float maxSpeed = 7;
    8.     public float jumpTakeOffSpeed = 7;
    9.  
    10.     private SpriteRenderer spriteRenderer;
    11.     private Animator animator;
    12.  
    13.     // Use this for initialization
    14.     void Awake ()
    15.     {
    16.         spriteRenderer = GetComponent<SpriteRenderer> ();  
    17.         animator = GetComponent<Animator> ();
    18.     }
    19.    
    20.  
    21.     protected override void ComputeVelocity()
    22.     {
    23.         Vector2 move = Vector2.zero;
    24.  
    25.         move.x = Input.GetAxis ("Horizontal");
    26.  
    27.         if (Input.GetButtonDown ("Jump") && grounded) {
    28.             velocity.y = jumpTakeOffSpeed;
    29.         } else if (Input.GetButtonUp ("Jump"))
    30.         {
    31.             if (velocity.y > 0) {
    32.                 velocity.y = velocity.y * 0.5f;
    33.             }
    34.         }
    35.  
    36.         bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (move.x < 0.01f));
    37.         if (flipSprite)
    38.         {
    39.             spriteRenderer.flipX = !spriteRenderer.flipX;
    40.         }
    41.  
    42.         animator.SetBool ("grounded", grounded);
    43.         animator.SetFloat ("velocityX", Mathf.Abs (velocity.x) / maxSpeed);
    44.  
    45.         targetVelocity = move * maxSpeed;
    46.  
    47.        
    48.     }
    49. }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    The horizontal axis range is -1 to 1 and you are setting it to flip the sprite if it is less than 0.01f when at rest the reading would be 0. So change
    Code (CSharp):
    1. bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (move.x < 0.01f));
    to
    Code (CSharp):
    1. bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (move.x < -0.01f));
    Although the old input system deals with deadzones for gamepads/controllers, I like to use a tolerance 20% myself so would suggest using 0.2f.