Search Unity

Question 2D Character not facing the direction of movement anymore (C#)

Discussion in '2D' started by SkrenZz, Jun 2, 2020.

  1. SkrenZz

    SkrenZz

    Joined:
    Apr 11, 2020
    Posts:
    26
    Hi there,

    I have an issue with 2D character movement and animation (I am also new to 2D games with characters). I've rigged and animated a character to be idle, run, and jump. Everything was working as expected and the character faced the direction of movement initially. Then, I decided to improve my jumping animation, and made the character's scale change so that it becomes slightly taller when jumping, and added a "bounce" animation which changes the character's scale to become shorter for a brief second and then back to its original height.

    Once I've made these improvements, the character doesn't face the right direction of movement anymore even when the idle/running animations are being played, despite the fact I haven't changed those.

    Here is the code responsible for the movement (happens in the Update function):
    Code (CSharp):
    1. if (movement > 0.01f)
    2.         {
    3.             this.animator.SetBool("isRunning", true);
    4.             this.animator.SetFloat("runSpeed", movement);
    5.  
    6.             this.transform.Translate(this.movementSpeed * movement * Time.deltaTime, 0f, 0f);
    7.             this.transform.localScale = this.leftScale;
    8.         }
    9.         else if (movement < -0.01f)
    10.         {
    11.             this.animator.SetBool("isRunning", true);
    12.             this.animator.SetFloat("runSpeed", -movement);
    13.  
    14.             this.transform.Translate(this.movementSpeed * movement * Time.deltaTime, 0f, 0f);
    15.             this.transform.localScale = this.rightScale;
    16.         }
    17.         else
    18.         {
    19.             this.animator.SetBool("isRunning", false);
    20.         }
    The values of this.leftScale and this.rightScale are initialized in the Start function:
    Code (CSharp):
    1.         this.leftScale = new Vector3(this.transform.localScale.x, this.transform.localScale.y, this.transform.localScale.z);
    2.         this.rightScale = new Vector3(this.transform.localScale.x * -1, this.transform.localScale.y, this.transform.localScale.z);
    3.  
    Since I am using the transform's scale property to make my character face the direction of movement, my questions are:
    1. Why is it no longer working even when animations in which the scale is not a field are being played?
    2. Is there a possibility to make the jump and bounce animations change a specific axis of the scale only?
    3. If the answer to question 2 is no, what other way could I hopefully resolve this issue?

    Many thanks in advance to everyone who'll try to help and have a great day!
     
  2. Sarashinai

    Sarashinai

    Joined:
    Jun 16, 2013
    Posts:
    20
    Just a simple starter suggestion. I've gone down rabbit holes many times trying to figure out "why did change x screw up already working system y" when it was actually some other change I made without really thinking about it.

    I'm not saying that this is definitely the case here but it's worth commenting out the changes you made to return to the original system before trying to figure out why it's not working. It's possible you made some other small change that's really causing the problem.

    Simplify and test.

    If it really is the scaling and translation that caused the problem, then you can go from there.
     
  3. SkrenZz

    SkrenZz

    Joined:
    Apr 11, 2020
    Posts:
    26
    Thank you very much Sarashinai, I debugged it further and found a solution at last. What solved the issue was adding two nested empty game objects (one to be manipulated in code to make my character face the direction of movement, and the other to be changed in animation) that contain all the objects of my character.

    After doing that I had to manually change all the paths of each and every animation property. This is no longer relevant to the issue for which I have posted this thread, but is there a more efficient way of doing it? I believe it is likely that there is (and if there isn't, I think they should add it and I will definitely suggest it to Unity), because changing the hierarchy of GameObjects is very frequently done.

    Thank you very much and have a great day!
     
  4. Sarashinai

    Sarashinai

    Joined:
    Jun 16, 2013
    Posts:
    20
    I'd suggest you post a new thread, or change this one from Help Wanted to Feedback, and then describe the issue in more detail. Screenshots may help where words can be ambiguous.