Mecanim Unity 3D for Beginners

Mecanim Unity 3D for Beginners

  • Unity 5 – From Beginner to Pro #39 – Mecanim (1) https://www.youtube.com/watch?v=I4Yr8_TU9SI
  • Unity 5 – From Beginner to Pro #40 – Mecanim (2) https://www.youtube.com/watch?v=08qfeGNZbR  w

  • Unity 5 – From Beginner to Pro #41 – Mecanim (3) https://www.youtube.com/watch?v=VpQO6QUPKLg 

001 Mechanim Unity 5

Raw mocap data for mecanim in the asset store

Import everything

  1. GameObject – 3D Object – plane
  2. Scale 25, 1, 25
  3. Drag the avatar into the scene
  4. 0, 0, 0 position
  5. Hit F to focus on the character (zooms in)
  6. Right click in the Project – Assets folder in the Raw Mocap Data Folder
  7. Create – Animator Controller
  8. Rename it MyAnimator
  9. Double Click on the MyAnimator and the Animator for this character opens


    ADD ANIMATIONS TO THE ANIMATOR
  10. Go to the Idle folder
  11. Drag IdleNeutral1 into the animator – This will be the starting point for any character (IDLE)
  12. Drag the animator onto the character
    Next Tutorial https://www.youtube.com/watch?v=08qfeGNZbRw
  13. Select main camera – GameObject – align with view
  14. Play to test

    NEW STATE
  15. Double click on the Animator
  16. Right click on an empty area and select CREATE STATE – NEW BLEND TREE
  17. Name it walkingforward
  18. Double click on the Walkingforward state
  19. Click the Plus icon.
  20. Add motion fields
  21. Select WalkForwardTurnRight_zNtrl_short
  22. Click the plus sign again
  23. Select WalkForward_NtrlFaceForward
  24. Click the plus sign again.
  25. Select WalkForwardTurnRight_zNtrl_short
  26. Place check in the mirror box in the motion inspector so your last character will turn to the left instead of the right
  27. Click to the left of the motion graph and make it -1
  28. In the lower right window, press play to preview the blend animation.
  29. You can also move the red slider bar in the motion graphic at the top left

https://www.youtube.com/watch?v=VpQO6QUPKLg
CREATE TRANSITIONS / SCRIPT TRANSITIONS

 

  1. Create two variables
  2. Click on the Parameters Tab
  3. Click the plus sign
  4. Select Float
  5. Rename it Speed
  6. Click on the plus sign again
  7. Rename it Direction

    CREATE A TRANSITION BETWEEN IDLE AND
  8. Right click over idle
  9. Make transition
  10. Drag the line over to the walk state
  11. Click on the little arrow in the middle of the transition line.

    NOW LIST A CONDITION FOR THE TRANSITION TO HAPPEN
  12. Click on the plus sign
  13. Drop Down and select Speed
  14. Put 0.1 in the box (speed is greater than 0.1)(this means that the character will be walking forward)
  15. Look above the graph for the words HAS EXIT TIME
  16. Uncheck (to disable) If you leave it on, then the animation will transition to another after a few seconds) We want the transition to occur only when it meets the condition.

    REPEAT THE PROCESS – MAKE A NEW TRANSITION (from walk forward to idle)
  17. Right click the mouse on the walk forward animation
  18. Select Make transition
  19. Drag it to the Idle default (orange) (which is the Idle state) (5:09)
  20. Click on the transition arrow in the center of the line
  21. Uncheck Has Exit
  22. Click the plus sign and select Speed from the dropdown
  23. Change the condition to LESS than 0.1

    NOW DIRECTION
  24. Double click on walk forward
  25. Click the drop down and select DIRECTION
  26. In the parameters tab, delete blend (because we’re not going to use it)
  27. Click on the Base Layer Tab

    CREATE NEW C# SCRIPT
  28. Right click – Create C# Script
  29. Name it PlayerController
  30. Click into the Scene
  31. Select the default avatar
  32. Drag the script into the Avatar’s Inspector
  33. Double click on the script to add some code to it. This will open MonoDevelop
  34. We need a reference to our Animator (8:34)
  35. Under the Public Class Type
  36. Private Animator
  37. Call it anim;


    private Animator anim;       //reference to the animator 
  38. Need two float variables, one for speed and one for direction

    private float playerDirection;        // variable to store direction coordinate values you will
                                                           receive from the players input
    private float playerSpeed;            // variable to store speed coordinates
                                                           receive from the players input 

NEED TO INITIALIZE THE VARIABLE

  1. Inside the Start Function type 

// Get the reference from the Animator
anim = GetComponent<Animator>( );       //get component from the current gameObject

 

  1. UPDATE FUNCTION

    This is where the change in states happen


// Get user input

playerDirection = input.GetAxis(“Horizontal”);

playerSpeed = input.GetAxis(“Vertical”);

anim.setFloat(“Direction”, playerDirection);

anim.setFloat(“Speed”, playerSpeed);

  1. Save  
  2. Go back to unity
  3. Press Play to Test
  4. Click the Front Arrow and the character should move