Search Unity

MakeHuman>Blender>Unity help

Discussion in 'Animation' started by StreetCodeStudios, Apr 15, 2015.

  1. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    Hello,
    I made a character in makehuman. Then exported to blender and exported .fbx and imported it in to unity and everythings seems ok. I give it humanoid rig, texture it and it looks good. Been trying to use raw mocap data package, I can't actually get the character to move and be animated. I've used an animator controller and if anyone can point me into the direction of a tutorial explaining that id be greatful. I want a third person character that's animated. I can get mostly everything working except the animations transitions. Sometimes I can't get the character to move but mostly I can without problem. Usually I can get the idle I want to work but that's as far as I can get. Can someone please explain the workflow? Or give hints or advice?

    Any help will be appreciated. Thanks.
     
  2. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Hmm, your issue is with the transitions? Are the individual animation states working fine? You mentioned that even the idle state can work usually.
     
  3. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    The animations work fine on the character. I've gotten the walk to work but he just constantly walks. Instead of when I move forward. I feel like this is simple I'm just missing something. I can't make it to where he default idles then when I move forward the walk animation plays them when I stop he goes back to idle and so on with other movements
     
  4. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Ok, if I get this correctly, you may want to check the Animator Controller's window to view the states. Create several states (each one loaded with the respective animation clips, such as idle, walk, jump, etc) and create transitions between them, which will allow the character to transition from idle to walk when you hold the walk key, for instance.

    The following link will help you get started in settin it up:
    http://docs.unity3d.com/Manual/class-AnimatorController.html

    Hope this helps ;)
     
    StreetCodeStudios likes this.
  5. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    I have looked at that manual and yes you are right that is what I'm wanting to do. Pretty much just a basic movable animated character. Do I have to code doing this? And one thing in lost on are parameters. I've seen tutorials use greater than 0.1 and less that 0.1 for transitions but they don't explain why and how it works.
     
  6. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Yes, you need code. The amount of the code will depend upon the complexity of your transitions to and from your various animations.

    You can refer to the sample project codes in the Unity Projects:
    http://unity3d.com/learn/tutorials/modules

    Try looking at Stealth and Survival Shooter.

    For the 0.1, are you referring to the exit time?
     
  7. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    Ive been trying to understand stealth. But I wasn't referring to exit times i was referring to some tutorials i watched and they would say for example

    transition from idle to walk the transition would occur when the float speed is greater than 0.1 and when you transition from walking to idle the float speed is less than 0.1. I understand what is being said and i know that's referring to the code but im not sure how to write the code and tell unity what to do. im not unfamiliar with c# but im a beginner.
     
  8. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Hey Joker - if you want a solution that doesn't require a left brain (artist) to learn the right brain (programmer) ways - you may want to check into playmaker on the asset store.

    Very nice for artists to accomplish tasks which normally require coding knowledge to complete.
     
  9. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.         // Cache the inputs.
    4.         float h = Input.GetAxis("Horizontal");
    5.         float v = Input.GetAxis("Vertical");
    6.         bool sneak = Input.GetButton("Sneak");
    7.      
    8.         MovementManagement(h, v, sneak);
    9.     }
    10.     void MovementManagement (float horizontal, float vertical, bool sneaking)
    11.     {
    12.         // Set the sneaking parameter to the sneak input.
    13.         anim.SetBool(hash.sneakingBool, sneaking);
    14.      
    15.         // If there is some axis input...
    16.         if(horizontal != 0f || vertical != 0f)
    17.         {
    18.             // ... set the players rotation and set the speed parameter to 5.5f.
    19.             Rotating(horizontal, vertical);
    20.             anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
    21.         }
    22.         else
    23.             // Otherwise set the speed parameter to 0.
    24.             anim.SetFloat(hash.speedFloat, 0);
    25.     }
    26.    
    Let's take the above for example.

    The SetFloat function is used to associate the parameter created in the Animation state transitions, which when met, will trigger the transition to occur, hence you get the character to begin walking from idle, for example.

    The 0.1 value you mentioned refers to the amount of input required to actually move the character, i.e. to make the animation go from idle to walk. Since the axis input will definitely achieve 0.1 (it eventually goes to 1), hence when you hold the movement keys, the character walks.

    Hope this elaboration manages to clear your doubts ;)
     
  10. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    Thank you for the descriptive elaboration. I haven't been able to use unity yet today but this give me more confidence ill be able to make something work. I'll post again soon.

    @Animator I've seen playmaker and thought about it but I know some art and some code already and I want to master both skills. I'm just having trouble on how to use c# in unity.
     
    theANMATOR2b likes this.
  11. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    My game is set up in a way that is slightly different than traditional with the navigation control and camera but still you control a humanoid and you move around an environment. I have my animator controller set up using raw mocap data and I have an idle and walk state. I have a parameter "Speed" and its a float. Transition from idle to walk is Speed greater than 0.1 and vice versa except less than. I'm using the first person controller script from character asset package in unity 5. I just have the camera third person and the mouse controls direction. I've tried numerous times making a script but it always fails. I can't get my humanoid to transition to the walk state but if I make walk the default state he walks.
     
  12. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    This shows that the animations run fine. When the default animation plays well, your controller is set up fine.

    Can you show your script which controls the walk movement? That way we can pinpoint your issue.
     
  13. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    Im using the First Person Controller that comes with unity 5. Should I use something different? I dont need all these things it comes with but im not sure on how to make a character controller completely yet either.

    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityStandardAssets.CrossPlatformInput;
    5. using UnityStandardAssets.Utility;
    6. using Random = UnityEngine.Random;
    7.  
    8. namespace UnityStandardAssets.Characters.FirstPerson
    9. {
    10.     [RequireComponent(typeof (CharacterController))]
    11.     [RequireComponent(typeof (AudioSource))]
    12.     public class FirstPersonController : MonoBehaviour
    13.     {
    14.         [SerializeField] private bool m_IsWalking;
    15.         [SerializeField] private float m_WalkSpeed;
    16.         [SerializeField] private float m_RunSpeed;
    17.         [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    18.         [SerializeField] private float m_JumpSpeed;
    19.         [SerializeField] private float m_StickToGroundForce;
    20.         [SerializeField] private float m_GravityMultiplier;
    21.         [SerializeField] private MouseLook m_MouseLook;
    22.         [SerializeField] private bool m_UseFovKick;
    23.         [SerializeField] private FOVKick m_FovKick = new FOVKick();
    24.         [SerializeField] private bool m_UseHeadBob;
    25.         [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    26.         [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    27.         [SerializeField] private float m_StepInterval;
    28.         [SerializeField] private AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
    29.         [SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
    30.         [SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.
    31.  
    32.         private Camera m_Camera;
    33.         private bool m_Jump;
    34.         private float m_YRotation;
    35.         private Vector2 m_Input;
    36.         private Vector3 m_MoveDir = Vector3.zero;
    37.         private CharacterController m_CharacterController;
    38.         private CollisionFlags m_CollisionFlags;
    39.         private bool m_PreviouslyGrounded;
    40.         private Vector3 m_OriginalCameraPosition;
    41.         private float m_StepCycle;
    42.         private float m_NextStep;
    43.         private bool m_Jumping;
    44.         private AudioSource m_AudioSource;
    45.  
    46.         // Use this for initialization
    47.         private void Start()
    48.         {
    49.             m_CharacterController = GetComponent<CharacterController>();
    50.             m_Camera = Camera.main;
    51.             m_OriginalCameraPosition = m_Camera.transform.localPosition;
    52.             m_FovKick.Setup(m_Camera);
    53.             m_HeadBob.Setup(m_Camera, m_StepInterval);
    54.             m_StepCycle = 0f;
    55.             m_NextStep = m_StepCycle/2f;
    56.             m_Jumping = false;
    57.             m_AudioSource = GetComponent<AudioSource>();
    58.             m_MouseLook.Init(transform , m_Camera.transform);
    59.  
    60.         }
    61.  
    62.  
    63.         // Update is called once per frame
    64.         private void Update()
    65.         {
    66.             RotateView();
    67.             // the jump state needs to read here to make sure it is not missed
    68.             if (!m_Jump)
    69.             {
    70.                 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    71.             }
    72.  
    73.             if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    74.             {
    75.                 StartCoroutine(m_JumpBob.DoBobCycle());
    76.                 PlayLandingSound();
    77.                 m_MoveDir.y = 0f;
    78.                 m_Jumping = false;
    79.             }
    80.             if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    81.             {
    82.                 m_MoveDir.y = 0f;
    83.             }
    84.  
    85.             m_PreviouslyGrounded = m_CharacterController.isGrounded;
    86.         }
    87.  
    88.  
    89.         private void PlayLandingSound()
    90.         {
    91.             m_AudioSource.clip = m_LandSound;
    92.             m_AudioSource.Play();
    93.             m_NextStep = m_StepCycle + .5f;
    94.         }
    95.  
    96.  
    97.         private void FixedUpdate()
    98.         {
    99.             float speed;
    100.             GetInput(out speed);
    101.             // always move along the camera forward as it is the direction that it being aimed at
    102.             Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
    103.  
    104.             // get a normal for the surface that is being touched to move along it
    105.             RaycastHit hitInfo;
    106.             Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    107.                                m_CharacterController.height/2f);
    108.             desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
    109.  
    110.             m_MoveDir.x = desiredMove.x*speed;
    111.             m_MoveDir.z = desiredMove.z*speed;
    112.  
    113.  
    114.             if (m_CharacterController.isGrounded)
    115.             {
    116.                 m_MoveDir.y = -m_StickToGroundForce;
    117.  
    118.                 if (m_Jump)
    119.                 {
    120.                     m_MoveDir.y = m_JumpSpeed;
    121.                     PlayJumpSound();
    122.                     m_Jump = false;
    123.                     m_Jumping = true;
    124.                 }
    125.             }
    126.             else
    127.             {
    128.                 m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    129.             }
    130.             m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
    131.  
    132.             ProgressStepCycle(speed);
    133.             UpdateCameraPosition(speed);
    134.         }
    135.  
    136.  
    137.         private void PlayJumpSound()
    138.         {
    139.             m_AudioSource.clip = m_JumpSound;
    140.             m_AudioSource.Play();
    141.         }
    142.  
    143.  
    144.         private void ProgressStepCycle(float speed)
    145.         {
    146.             if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    147.             {
    148.                 m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    149.                              Time.fixedDeltaTime;
    150.             }
    151.  
    152.             if (!(m_StepCycle > m_NextStep))
    153.             {
    154.                 return;
    155.             }
    156.  
    157.             m_NextStep = m_StepCycle + m_StepInterval;
    158.  
    159.             PlayFootStepAudio();
    160.         }
    161.  
    162.  
    163.         private void PlayFootStepAudio()
    164.         {
    165.             if (!m_CharacterController.isGrounded)
    166.             {
    167.                 return;
    168.             }
    169.             // pick & play a random footstep sound from the array,
    170.             // excluding sound at index 0
    171.             int n = Random.Range(1, m_FootstepSounds.Length);
    172.             m_AudioSource.clip = m_FootstepSounds[n];
    173.             m_AudioSource.PlayOneShot(m_AudioSource.clip);
    174.             // move picked sound to index 0 so it's not picked next time
    175.             m_FootstepSounds[n] = m_FootstepSounds[0];
    176.             m_FootstepSounds[0] = m_AudioSource.clip;
    177.         }
    178.  
    179.  
    180.         private void UpdateCameraPosition(float speed)
    181.         {
    182.             Vector3 newCameraPosition;
    183.             if (!m_UseHeadBob)
    184.             {
    185.                 return;
    186.             }
    187.             if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    188.             {
    189.                 m_Camera.transform.localPosition =
    190.                     m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    191.                                       (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    192.                 newCameraPosition = m_Camera.transform.localPosition;
    193.                 newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    194.             }
    195.             else
    196.             {
    197.                 newCameraPosition = m_Camera.transform.localPosition;
    198.                 newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    199.             }
    200.             m_Camera.transform.localPosition = newCameraPosition;
    201.         }
    202.  
    203.  
    204.         private void GetInput(out float speed)
    205.         {
    206.             // Read input
    207.             float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    208.             float vertical = CrossPlatformInputManager.GetAxis("Vertical");
    209.  
    210.             bool waswalking = m_IsWalking;
    211.  
    212. #if !MOBILE_INPUT
    213.             // On standalone builds, walk/run speed is modified by a key press.
    214.             // keep track of whether or not the character is walking or running
    215.             m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    216. #endif
    217.             // set the desired speed to be walking or running
    218.             speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    219.             m_Input = new Vector2(horizontal, vertical);
    220.  
    221.             // normalize input if it exceeds 1 in combined length:
    222.             if (m_Input.sqrMagnitude > 1)
    223.             {
    224.                 m_Input.Normalize();
    225.             }
    226.  
    227.             // handle speed change to give an fov kick
    228.             // only if the player is going to a run, is running and the fovkick is to be used
    229.             if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    230.             {
    231.                 StopAllCoroutines();
    232.                 StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    233.             }
    234.         }
    235.  
    236.  
    237.         private void RotateView()
    238.         {
    239.             m_MouseLook.LookRotation (transform, m_Camera.transform);
    240.         }
    241.  
    242.  
    243.         private void OnControllerColliderHit(ControllerColliderHit hit)
    244.         {
    245.             Rigidbody body = hit.collider.attachedRigidbody;
    246.             //dont move the rigidbody if the character is on top of it
    247.             if (m_CollisionFlags == CollisionFlags.Below)
    248.             {
    249.                 return;
    250.             }
    251.  
    252.             if (body == null || body.isKinematic)
    253.             {
    254.                 return;
    255.             }
    256.             body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    257.         }
    258.     }
    259. }
    260.  
     
  14. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    With Mecanim, you don't really need the character controller. Well you can certainly modify the given script to cater to your needs, but it's better to start fresh from scratch.

    You can modify the code I attached this way:

    Code (CSharp):
    1. void FixedUpdate ()
    2.     {
    3.         // Cache the inputs.
    4.         float h = Input.GetAxis("Horizontal");
    5.         float v = Input.GetAxis("Vertical");
    6.         MovementManagement(h, v);
    7.     }
    8.     void MovementManagement (float horizontal, float vertical)
    9.     {
    10.         // If there is some axis input...
    11.         if(horizontal != 0f || vertical != 0f)
    12.         {
    13.             Animator.SetFloat("Speed", 1.0f, speedDampTime, Time.deltaTime);
    14.         }
    15.         else
    16.             // Otherwise set the speed parameter to 0.
    17.             Animator.SetFloat("Speed", 0);
    18.     }
     
  15. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    I got this error as soon as i put it in
    error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetFloat(string, float)'
     
  16. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Oops, sorry about the typo. Use animator.SetFloat instead of capital A
     
  17. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    When I change the capital A to lower case I get
    error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetFloat(string, float)'
    error CS0103: The name `animator' does not exist in the current context

    Ive wrote a couple scripts similar to this and i always got errors. could unity be setup wrong?
    I really appreciate the help though. it has helped me not to give up.
     
  18. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    you need to cache animator (or any name u prefer) like this:
    void Awake ()
    {
    animator = GetComponent<Animator>();
    }
     
  19. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    I tried Void Start () when i first got the error and did the same thing now i tried Void Awake() and ive got

    error CS0103: The name `animator' does not exist in the current context * 2
    and
    error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetFloat(string, float)'

    Heres the code
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     void Awake()
    8.     {
    9.         animator = GetComponent<Animator> ();
    10.     }
    11.  
    12.  
    13.     void FixedUpdate ()
    14.     {
    15.         // Cache the inputs.
    16.         float h = Input.GetAxis("Horizontal");
    17.         float v = Input.GetAxis("Vertical");
    18.         MovementManagement(h, v);
    19.     }
    20.     void MovementManagement (float horizontal, float vertical)
    21.     {
    22.         // If there is some axis input...
    23.         if(horizontal != 0f || vertical != 0f)
    24.         {
    25.             animator.SetFloat("Speed", 1.0f, speedDampTime, Time.deltaTime);
    26.         }
    27.         else
    28.             // Otherwise set the speed parameter to 0.
    29.             Animator.SetFloat("Speed", 0);
    30.     }
    31. }
    32.  
     
  20. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    I just changed this animator reference to lower case and now i just have 3 errors
    Code (csharp):
    1.  
    2.         else
    3.             // Otherwise set the speed parameter to 0.
    4.             animator.SetFloat("Speed", 0);
    5.     }
    6. }
    7.  
    error CS0103: The name `animator' does not exist in the current context * 3
     
  21. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    I just got it!! Thanks so much!! This was the motivation I needed! You have helped me tremendously. I made a public float called speedDampTime and a public Animator Anim. Heres the final code.
    Code (csharp):
    1.  
    2. public class PlayerMovement : MonoBehaviour {
    3.  
    4.     public Animator Anim;
    5.     public float speedDampTime = 0.1f;
    6.  
    7.  
    8.     public void Awake()
    9.     {
    10.         Anim = GetComponent<Animator> ();
    11.     }
    12.  
    13.  
    14.     public void FixedUpdate ()
    15.     {
    16.         // Cache the inputs.
    17.         float h = Input.GetAxis("Horizontal");
    18.         float v = Input.GetAxis("Vertical");
    19.         MovementManagement(h, v);
    20.     }
    21.     public void MovementManagement (float horizontal, float vertical)
    22.     {
    23.         // If there is some axis input...
    24.         if(horizontal != 0f || vertical != 0f)
    25.         {
    26.             Anim.SetFloat("Speed", 1.0f, speedDampTime, Time.deltaTime);
    27.         }
    28.         else
    29.             // Otherwise set the speed parameter to 0.
    30.             Anim.SetFloat("Speed", 0);
    31.     }
    32. }
    33.  
     
  22. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    Glad you figured it out on your own ;):cool:
     
  23. StreetCodeStudios

    StreetCodeStudios

    Joined:
    Aug 30, 2013
    Posts:
    21
    Thats the most rewarding way of doing something!
    The character still rotates in a way i dont like but im working with it.
    Theres still alot more i need to learn how to do but this definitely enhances my skills.
    Thanks again!