Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to properly set animation code (walking, running, sideStrafe)

Discussion in 'Scripting' started by gasguirre, Jul 5, 2020.

  1. gasguirre

    gasguirre

    Joined:
    Aug 7, 2018
    Posts:
    5
    Hello,

    I'm dealing with trying to set animations along with player movement. however I'm not sure about what am I doing and if it's not over complicating the code.
    I wanted to put animations for idle/walking/joggin/running
    and also for rightSideStrafe and leftSideStrafe
    crouch, etc.

    right now this is the code I have for the UpdateAnimator()
    but I don't know if this would be the best way to do it. or even if will accomplish what I need. it is working well for forwards and backwards animations, but I can't make it work for sideStrafe.

    private void UpdateAnimator()
    {
    float speed = m_smoothCurrentSpeed;


    if (m_smoothInputVector.x > 0)
    {
    GetComponentInChildren<Animator>().SetFloat("sideSpeed", speed);
    }
    else
    {
    GetComponentInChildren<Animator>().SetFloat("sideSpeed", -speed);
    }



    if (m_smoothInputVector.y > 0)
    {
    GetComponentInChildren<Animator>().SetFloat("forwardSpeed", speed);
    }
    else
    {
    GetComponentInChildren<Animator>().SetFloat("forwardSpeed", -speed);
    }
    }


    and here some screenshots of the animator.

    upload_2020-7-5_19-36-33.png




    sideStrafe:

    upload_2020-7-5_19-36-56.png
    upload_2020-7-5_19-37-6.png

    and Locomotion:

    upload_2020-7-5_19-37-25.png
    upload_2020-7-5_19-37-38.png


    the Locomotion one seems to be working well, however I feel that the code is not well written and could be better formatted to adjust to future animations/cases
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    Hm, it kinda looks superficially reasonable... I suggest perhaps you drag the Animator window to one side and run your game and try to look at the different states to track down where it is going under certain conditions. It could be it is in one of your states that does not have a transition out based on the variable you're using for strafe.

    Make sure your state transition thresholds are reachable too... usually 0.1 or 0.5 is used rather than 1.0, because on a circular controller it maybe be impossible to reach precisely 1.0... you might be at 0.99999 motion and that might print out as 1.0 (because of rounding), but mathematically be less than 1.0, so the thresholds is never reached.
     
  3. gasguirre

    gasguirre

    Joined:
    Aug 7, 2018
    Posts:
    5
    that sounds sensible, I'l try that and update. thank you for pointing out the 1.0 thing, is true, I need to try to do it thinking also in controller situations.. maybe this can be causing the issue.

    thank you!
     
  4. gasguirre

    gasguirre

    Joined:
    Aug 7, 2018
    Posts:
    5
    uhm... I'm thinking that my problem could be caused for using a third party solution for the first person controller. I'll work on replicating with a simple control in a new project, because so far the sidestrafe animations don't seem to trigger (they do in inspector but they don't show)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    Interesting. Make sure you don't have two animators/animations fighting it out over the same transforms.

    What if you make a fresh scene with ALL Scripts removed, leave the animators, then make a test script that sets those strafe properties. Does the animation fire in that case?