Search Unity

Can't figure out how to fix this correctly

Discussion in '2D' started by bradleymcmanus, Feb 21, 2018.

  1. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    Ok. I can make this code work, but I don't think that I'm doing it correctly. Also, I think my code could be tweaked as I'm new and still learning. Everything I know I've learn from youtube tutorials. Here's my code.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     public float moveSpeed;
    7.  
    8.     private Animator anim;
    9.  
    10.     private int idle_FaceX;
    11.     private int idle_FaceY;
    12.  
    13.  
    14.  
    15.  
    16.     void Start()
    17.     {
    18.        
    19.  
    20.         anim = GetComponent<Animator>();
    21.  
    22.     }
    23.  
    24.  
    25.     void Update () {
    26.  
    27.         anim.SetBool("PlayerMoving", false);
    28.         idle_FaceX.Equals(0);
    29.         idle_FaceY.Equals(0);
    30.  
    31.         if (Input.GetAxisRaw("Horizontal") > 0f)
    32.         {
    33.             anim.SetBool("PlayerMoving", true);
    34.  
    35.             idle_FaceX.Equals(1);
    36.  
    37.             idle_FaceY.Equals(0);
    38.  
    39.             transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
    40.         }
    41.  
    42.         if (Input.GetAxisRaw("Horizontal") < 0f)
    43.         {
    44.             anim.SetBool("PlayerMoving", true);
    45.  
    46.             idle_FaceX.Equals(-1);
    47.  
    48.             idle_FaceY.Equals(0);
    49.  
    50.             transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
    51.         }
    52.  
    53.         if (Input.GetAxisRaw("Vertical") > 0f)
    54.         {
    55.             anim.SetBool("PlayerMoving", true);
    56.  
    57.             idle_FaceY.Equals(1);
    58.  
    59.             idle_FaceX.Equals(0);
    60.  
    61.             transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
    62.  
    63.         }
    64.         if (Input.GetAxisRaw("Vertical") < 0f)
    65.         {
    66.             anim.SetBool("PlayerMoving", true);
    67.  
    68.             idle_FaceY.Equals(-1);
    69.  
    70.             idle_FaceX.Equals(0);
    71.  
    72.             transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
    73.         }
    74.  
    75.  
    76.  
    77.  
    78.  
    79.         anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
    80.  
    81.         anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
    82.  
    83.         anim.SetFloat("LastMoveX", idle_FaceX);
    84.  
    85.         anim.SetFloat("LastMoveY", idle_FaceY);
    86.  
    87.     }
    88. }
    89.  
    The current problem I'm having is getting my 2 anim LastMove floats to take the values of my 2 idle_face ints. I was able to work around it by using anim.setfloat inside of the 'if' function, like I've done with the anim bool PlayerMoving but I would like to be able to set those with variables I've created. Also, any advice or questions on the script as a whole are both welcomed and encouraged. Thanks ahead of time!
     
  2. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    I think the problem is that you are trying to asign a integer value where a float value is expected.
    you could use a type cast

    anim.SetFloat("LastMoveY", (float) idle_FaceY);

    But before you use a type cast you should think about if you could just change the used type in your script and animator. Do you realy need a float value in the animator? If not, the change the parameter in the animator to an int type and use anim.SetInt() .... or do you have to use an int value in your script? Maybe you could use a float instead of integer.
     
  3. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    I see what you're saying. I was following a tutorial which is why I used the float type. I don't see why I couldn't use a float, but I don't think that will fix the problem I'm having. To be more specific, lets use the bool as an example. I created that using unity inside the animator. What I'm trying to do is change that (the anim.bool) using my own variable. Something like "anim.setbool("{unity created bool}", {bool created in script}); From what I've seen, that line of code should take my script created bool and apply the value to the unity created bool so that I may use it in the animator. Problem is, it doesn't work. That's why I have the anim.setbool under the if functions so that its just changed directly. Same problem applies to the anim.setfloat values. If I set them under the if function, it works. I just don't think that's the best way to do it as I may want to manipulate those values across multiple game objects. Sorry if this doesn't make a lot of sense, I'm still learning terminology and such.

    **edit to add**

    Also, I should mention that when trying to change those floats before, I did something similar to your suggestion. I used vector2 .x and .y to try to impart those values and had no success.
     
  4. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    If I understand you correctly you are saying that your

    anim.SetFloat("LastMoveX", idle_FaceX);

    doesn't set the Animation Float as it should?

    It look like you don't set those variables in the script at all. Why are you using the equals() function?
    This function just checks if the variable is equal to the value you pass in as parameter and returns a boolean. It makes no sense to use this like you do. If you would like to set idle_FaceX to p.E. to -1 you have to do it this way

    idle_FaceX = -1;
     
    bradleymcmanus likes this.
  5. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    The idle_Face variables were my attempt at a work around. I was using the equals() function to set the values based on the if results. Obviously that's not the correct way to use that function, thanks for that. The original code was different but it didn't work for me so I was trying to fix it myself. Basically, what I'm trying to do is set a value that will tell the animator which way I was last facing (up, down, left, or right) so that I can play an animation based on that information. I would like to store that information in a variable that I can then call to the animator using anim.set followed by my script created variable. I'm not opposed to using something other than input.getaxisraw for my movement either if there's a better way.
     
  6. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Are you working in a 3D or a 2D enviorment?
     
  7. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
  8. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    If you are working on something like a 2D sidescrolling game, a very easy way to work with the looking direction is by flipping the spriteRenderer axis. You can just flipp your animation by using the flipX oder flipY attribute of the spriteRenderer. Of course this works only when your sprites are not to specific.
     
  9. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    2D top down, sorry. Not side scrolling.
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This code:
    Code (csharp):
    1. idle_FaceX.Equals(0);
    2. idle_FaceY.Equals(0);
    Is probably not doing what you think it's doing? This returns a bool saying if the value matches.
    Just write this to assign:
    Code (csharp):
    1. idle_FaceX = 0;
     
  11. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20

    That is correct. I was told that's not the right way to do that. Still trying to figure out a solution to my problem.
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah, I'm sorry, I missed that that was already noticed earlier in the thread. :)

    What is the current issue you have? :)

    Code (csharp):
    1.  
    2. void Update() {
    3. float h = Input.GetAxisRaw("Horizontal");
    4. float v = Input.GetAxisRaw("Vertical");
    5.  
    6. transform.Translate(new Vector3(h,v).normalized * moveSpeed * Time.deltaTime);
    7.  
    8. anim.SetFloat("MoveX", h);
    9. anim.SetFloat("MoveY", v);
    10. /* do you only want to update these next 2 variables if you've moved, and not update them if you haven't? */
    11. if(h != 0)
    12.    anim.SetFloat("LastMoveX", h);
    13. if(v != 0)
    14.    anim.SetFloat("LastMoveY", v);
    15. anim.SetBool("PlayerMoving", (v != 0) || (h != 0));
    That may be how I would condense that. Though, I still may not know your issue.
     
    Last edited: Feb 21, 2018
    Verdemis likes this.
  13. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    I'm trying to make a basic movement script that allows for walking animation as well as keeping you faced in the last direction you were walking when no movement is detected. My code is an example of both my work around to force it to work, and the way I'd like it to work. anim.setbool under the if function is my work around. Writing the code that way works. anim.setfloat is the way I'd like to do it. I'd rather set the value created in the unity animator by applying my own script created variable to it. I hope that makes sense.
     
  14. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    What I'm looking for is how to change my anim variables in script by applying my own script created variables to them. ex. anim.SetFloat("LastMoveX", idle_FaceX); where LastMoveX is the variable I created in the unity animator and idle_FaceX is the variable I created in script.
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I edited my post to only update the value if you've moved. I think that covers what you talked about entirely?
     
    Verdemis likes this.
  16. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    Some of it confuses me. Let me look some stuff up and try it out. I'll get back to you though! Thanks for your help!
     
  17. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    anim.SetBool("PlayerMoving", (v != 0) || (h != 0));

    How is true or false determined here? Just trying to further understand the code as I'm teaching myself haha. I understand the rest of your code and think it will work. It's also shown me some different ways to look at things. I appreciate it.
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That one piece of code is just saying that the player is moving so long as 'v' or 'h' isn't 0.
     
  19. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    I get it now. Thanks again!
     
  20. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    I've plugged that code in and it's working as it should with one exception. In order to properly select which animation to use in idle, the LastMoveX and LastMoveY need to be able to reset to 0 if there's no input. That way I'm left with 1 or -1 on either the X or Y axis and 0 on the other as I want to choose 1 of 4 idle animations (up, down, left and right).
     
  21. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah okay, so if you have up and right which idle pose is chosen? With only 4 animation idle poses, I mean.

    Code (csharp):
    1. if(v != 0 || h !=0) {
    2.    anim.SetFloat("LastMoveY", v);
    3.    anim.SetFloat("LastMoveX", h);
    4.   }
    Maybe like that?
     
    Last edited: Feb 22, 2018
  22. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    None at the moment, which is why this step is even necessary lol. Still learning.
     
  23. bradleymcmanus

    bradleymcmanus

    Joined:
    Feb 21, 2018
    Posts:
    20
    With this step, the very last cardinal direction is the only one it takes, so it will play the idle animation for left, right, up or down. Thanks again, you've helped me better understand how to do this.