These are all of the State Machines and Blend Trees that I set up for my character in Mecanim:

Showing posts with label Unity Mecanim. Show all posts
Showing posts with label Unity Mecanim. Show all posts
Saturday, March 23, 2019
Friday, March 22, 2019
Demo Video Presentation
This is my presentation, showing off my project thus far, all of the state machines, briefly going over how I utilized Unity IK to create all of the functionality in my character controller in this project:
Part 01
Part 02
Part 03
Part 04
Part 05
AIM IK Fix and Hand to Wall IK
I have made many adjustments since my last post. I fixed the foot turning animation on the lower half of the body, it now only plays when player is in idle, standing still, and looking left and right. I fixed aiming, it only activates when the player is shooting, to avoid the awful sideways over-bending of the body, and the twisting and contorting that was occurring earlier. The Aim IK weight is set to 0, unless player is firing and strafing. Next, I added a muzzle flash effect from the asset store, to highlight the presence of the character's weapon in 3rd person view, since it isn't easily visible. I added a coroutine in the script to give short delay so that player character isn't firing before the weapon is fully raised.
Finally, I have successfully built the functionality to get the character to detect and touch a wall on his left with his offhand using a raycast and IK! There is still a minor problem, since the hand doesn't make direct contact with the wall. Still, this lays the foundation for all other wall-touching actions on my task list such as creating friction while falling and ledge grabbing. However, I don't think I will get to ledge grabbing, that'll be a bit tricky, but friction while falling is worth a try! This may also pave the way for me to troubleshoot foot planting, since they both use raycasts and animation curves to blend between IK and an animation.
Above, we can see the raycast detecting the wall and causing the animation to play. On the right, we can see the original animation in the inspector, and a curve above it. This curve is what sets the weight for blending between Unity IK and the original animation.
Thursday, March 21, 2019
Aiming System--Unity IK
Aiming is almost completely implemented, but is acting strange because of how the character controller and look IK conflict when they control the character. Foot planting is taking a hiatus--for now.

Here is the updated IK Control script in the inspector, showing the parameters that have been added for controlling the look IK.

I will have to play with these in the script, or the character controller itself in order to resolve the issues.
Foot Planting
I have been working on getting foot planting working. I have come much further since my last attempt at IK foot planting, but I am still not quite there yet. Before, I could not get it working. Then I got some jittery collision with the ground. Now, the feet are stuck down at all times, even when airborne, which is not desirable either.
Bug Fixing
So, I finished modifying the character controller code, reverting back to the original method I was using to control the character and camera, and the problem still persists. It appears to be controlling some of the IK handles in local space, and others in world space, which is very problematic.
I checked the documentation, and found that the function for moving an IK handle in space, "Animator.SetIKPosition" only works in world space by default, and there seems to be no way in the function to set it to local space. Even though I had parented the IK handles, they were still moving around in world space, making it impossible to control my character's hands' positions relative to the character. I noticed that the only IK handle that was cooperating was the left hand grab handle, located deep in the hierarchy, parented to the rifle. I have no idea why this is the case. If parenting causes IK handles to work in local space, then they should, no matter how dep in the character's hierarchy they are parented. I have parented all of the hand IK locator objects to the rifle in the character's right hand, which may produce better results anyway.
It did not achieve better results, because they are parented beneath the root bones in the hierarchy, and therefore, they don't aim directly to the center of the screen, but rather wherever the character is facing in the animation, which causes the character to aim to the right whenever I press the fire button, instead the center of the screen, which is not what I want.
I re-parented the right hand IK locator objects, which control the shooting, to the character rig, and somehow it is miraculously working. The character's hands and rifle now stay where they are supposed to be relative to the character's position/rotation in space. I have also successfully rewritten my IK Control script.
So, I am back where I was before with the hands instantaneously moving to the positions of the IK handles. The coroutine waitforseconds values are also the same as before, to try and fake smoothness. I tried multiple methods to translate the handles or to somehow blend the weights, none of them worked. So, now it is time to move on and build other features. As you may notice, I also have a test environment built using Sabre CSG, a free Unity Asset Store plugin that works similar to Unreal's built-in level builder tool, using boolean tools I will build on this test environment as needed and use it to rest my character's abilities. We will see more of it later:
Here is the new IK Control script:
I checked the documentation, and found that the function for moving an IK handle in space, "Animator.SetIKPosition" only works in world space by default, and there seems to be no way in the function to set it to local space. Even though I had parented the IK handles, they were still moving around in world space, making it impossible to control my character's hands' positions relative to the character. I noticed that the only IK handle that was cooperating was the left hand grab handle, located deep in the hierarchy, parented to the rifle. I have no idea why this is the case. If parenting causes IK handles to work in local space, then they should, no matter how dep in the character's hierarchy they are parented. I have parented all of the hand IK locator objects to the rifle in the character's right hand, which may produce better results anyway.
It did not achieve better results, because they are parented beneath the root bones in the hierarchy, and therefore, they don't aim directly to the center of the screen, but rather wherever the character is facing in the animation, which causes the character to aim to the right whenever I press the fire button, instead the center of the screen, which is not what I want.
I re-parented the right hand IK locator objects, which control the shooting, to the character rig, and somehow it is miraculously working. The character's hands and rifle now stay where they are supposed to be relative to the character's position/rotation in space. I have also successfully rewritten my IK Control script.
So, I am back where I was before with the hands instantaneously moving to the positions of the IK handles. The coroutine waitforseconds values are also the same as before, to try and fake smoothness. I tried multiple methods to translate the handles or to somehow blend the weights, none of them worked. So, now it is time to move on and build other features. As you may notice, I also have a test environment built using Sabre CSG, a free Unity Asset Store plugin that works similar to Unreal's built-in level builder tool, using boolean tools I will build on this test environment as needed and use it to rest my character's abilities. We will see more of it later:
Here is the new IK Control script:
Bug Fixing
I realized that in the Animator window, clicking on the animation in question and ticking the "Foot IK" checkbox helps eliminate foot sliding during animations. Of course, there is still some foot sliding during the transitions, but that is unavoidable.
I rewrote my IK Solver, it still didn't work. For some freak reason, the IK locators are not obeying parenting conventions all of a sudden. This can be seen in the previous post where I show that instead of punching or aiming in front of himself, the character is punching in different directions as I move him around, because the IK locators seem to be in world space, rather than local space, relative to the character.
It may be related to how the character is picking up the camera's rotation whenever the W or left mouse keys are pressed. However, this does not make since, since the IK locators are parented to the character's rig, beneath the object that is rotating with the direction the camera is facing. I have to find a way to automate all of the IK handle's positions, or I may have to abandon IK for the remainder of this project. If I do the latter, I will later evaluate how to properly use Unity IK, and just focus on getting as many animations in as possible and using animation layers.
I rewrote my IK Solver, it still didn't work. For some freak reason, the IK locators are not obeying parenting conventions all of a sudden. This can be seen in the previous post where I show that instead of punching or aiming in front of himself, the character is punching in different directions as I move him around, because the IK locators seem to be in world space, rather than local space, relative to the character.
It may be related to how the character is picking up the camera's rotation whenever the W or left mouse keys are pressed. However, this does not make since, since the IK locators are parented to the character's rig, beneath the object that is rotating with the direction the camera is facing. I have to find a way to automate all of the IK handle's positions, or I may have to abandon IK for the remainder of this project. If I do the latter, I will later evaluate how to properly use Unity IK, and just focus on getting as many animations in as possible and using animation layers.
Another option is to keep the parenting of the camera and character controller and all components, but revert to how the camera and character rotation was originally being controlled (in the scripts). Right now, the camera rotates around the character, and the character only faces where the camera is if being moved with one of the W,S,A,or D keys, or when the left mouse button is pressed. Originally, the mouse was controlling the rotation of the character on the Y axis (horizontal).
I feel like I am finally getting to the point where I am grasping and understanding Unity IK, that is, how the IK C# functions work, what they are doing, and how to use them. However, there are still things that I do not understand.
Wednesday, March 20, 2019
Bug Fixing
I have fixed most of the bugs that I was having earlier.
Re-parenting the camera and character controller components again worked as I had predicted. All components are now on the topmost object in the hierarchy, Hunter Transform.
I have fixed the root rotation issue where the character was rotating downward with the camera. The way I fixed it is setting the camera object in the movement controller as the camera pivot null object that the camera is a child of instead of the camera itself. I did this because the camera pivot is not pointing downward, but has all of it's rotations zeroed so that it is aligned with the floor plane.
I am still having a problem with the IK, but worse. Now, the IK is completely broken, and I have no idea why. The aim IK keeps flickering on and off, creating a jittery motion. When I move the character around in play mode, the IK locator objects seem to be moving in world space so that the character does not punch in the correct direction. However, this should not be happening since they are parented to the character and not in world space. Observe:
This is a very perplexing and confounding issue. I really hope that I do not need to re-setup the null objects, or rewrite the IK script.
Re-parenting the camera and character controller components again worked as I had predicted. All components are now on the topmost object in the hierarchy, Hunter Transform.
I have fixed the root rotation issue where the character was rotating downward with the camera. The way I fixed it is setting the camera object in the movement controller as the camera pivot null object that the camera is a child of instead of the camera itself. I did this because the camera pivot is not pointing downward, but has all of it's rotations zeroed so that it is aligned with the floor plane.
I am still having a problem with the IK, but worse. Now, the IK is completely broken, and I have no idea why. The aim IK keeps flickering on and off, creating a jittery motion. When I move the character around in play mode, the IK locator objects seem to be moving in world space so that the character does not punch in the correct direction. However, this should not be happening since they are parented to the character and not in world space. Observe:
This is a very perplexing and confounding issue. I really hope that I do not need to re-setup the null objects, or rewrite the IK script.
Monday, March 18, 2019
State Machines
Here are all of my state machines.
This one is for the base layer, for locomotion. Aside from a bunch of unused states floating around, we can see two blend trees, and a bunch of other states. These two blend trees handle character movement in all directions, and character movement while shooting. They then transition into jumping, which begins with an initial takeoff animation, into a midair loop, and finally the landing animations. The midair jump can also transition into a shooting midair looping animation if the player decides to fire while airborne. The landing animations then transition back into the idle and movement animations.
Here is a closer look at the Blend Tree for the idle state, base character locomotion, and sprinting. The one for strafing is just like it, except it has the animations for strafing while shooting instead.
Last, is a layer that masks over the hands, keeping them gripping the rifle. This will be expanded to include the different hand motions such as punching and grabbing:
This one is for the base layer, for locomotion. Aside from a bunch of unused states floating around, we can see two blend trees, and a bunch of other states. These two blend trees handle character movement in all directions, and character movement while shooting. They then transition into jumping, which begins with an initial takeoff animation, into a midair loop, and finally the landing animations. The midair jump can also transition into a shooting midair looping animation if the player decides to fire while airborne. The landing animations then transition back into the idle and movement animations.
Next, is the layer, masking over the upper body which controls aiming the rifle:
Blend Tree for upper body movement during locomotion:
Blend Tree for rifle aiming and firing, blends vertically between aiming up and down:
Last, is a layer that masks over the hands, keeping them gripping the rifle. This will be expanded to include the different hand motions such as punching and grabbing:
This Week's Progress in Unity
This week I created a camera controller, re-parenting several components of my character game object's hierarchy in the scene:
Earlier, the camera was parented inside of the character, and all of the components including scripts were on the outermost object in the hierarchy. Now, there is a parent null object underneath which the character rig and camera reside in two separate game objects. The camera is now parented underneath a null object resting at the pivot point of the character, (0, 0, 0) in local space.
It's nice that my character now has a decent camera controller, however, his functionality is now suffering from a few bugs. First, the character rotates downward after moving. This is because I set the characters transform.rotation to be equal with that of the camera, and the camera is looking downward. I will have to find a way to rotate the character back up so on the x axis so that its forward vector is parallel with that of the world's.
The second, I think, is due to the re-parenting I did of the camera and character controller, because the colliders are no longer working, so the landing animations no longer play. I will re-parent the camera, and apply the animator component and all other scripts and components to the top/root level of the hierarchy.
The last issue I have to fix is that the IK aiming is also malfunctioning, flickering instead of staying constant. The left hand is no longer moving to the position of the ik handle on the rifle. I don't know why this is taking place, but the issue started when I re-parented the camera controller object, if that is any clue.
I have started to build the functionality for the character's melee system. There is a null object that the left-hand IK handle snaps to when it is toggled active. Right now it's a bit choppy, but I will use this to give my character the ability to strike at targets, his fist automatically moving to contact. I will need to figure out how to achieve smooth blending from null objects to IK handles for all Unity IK functionality in order for the animations to be believable.
Sunday, March 10, 2019
Further Work on Aiming and Jumping
Here, I demonstrate my jumping mechanic and rifle aiming. Both are still in progress, this is an update to show the advancements that have been made so far.
I have used Unity IK to correct the position of the characters hands and rifle while aiming, so that he aims completely forward, in the direction of the camera. Some additional work is required because his hands freeze in that position, which looks awkward, and his torso also needs to be rotated to match where the rifle is aiming. There is also no rotation in the Y axis, and no animation for when he is idle and turning left and right with the camera. I will fix those issues as well to create a more animated character.
I show here my character in a test environment, downloaded from the Unity Asset store. This is just to have scale reference, and to also be able to see how fast he is moving, how high he is jumping, etc. This is where I demonstrate my jumping mechanic so far. He now falls faster and can jump higher or lower depending on how long the player holds the space bar. I still plan on adding two more types of landing, the hard landing, and rolling. I will also add jumping in all directions, because right now he only jumps forward. In addition, a shooting midair idle animation that can be transitioned to while the player is somersaulting through the air is needed.
I have used Unity IK to correct the position of the characters hands and rifle while aiming, so that he aims completely forward, in the direction of the camera. Some additional work is required because his hands freeze in that position, which looks awkward, and his torso also needs to be rotated to match where the rifle is aiming. There is also no rotation in the Y axis, and no animation for when he is idle and turning left and right with the camera. I will fix those issues as well to create a more animated character.
I show here my character in a test environment, downloaded from the Unity Asset store. This is just to have scale reference, and to also be able to see how fast he is moving, how high he is jumping, etc. This is where I demonstrate my jumping mechanic so far. He now falls faster and can jump higher or lower depending on how long the player holds the space bar. I still plan on adding two more types of landing, the hard landing, and rolling. I will also add jumping in all directions, because right now he only jumps forward. In addition, a shooting midair idle animation that can be transitioned to while the player is somersaulting through the air is needed.
Monday, March 4, 2019
Getting "Apply Root Motion" Checked and Re-Structuring My Project
I had "Apply Root Motion" unchecked. That was causing some problems as we saw before. There were two main problems that I was having when apply root motion was checked, First, the camera would spin like a carousel around the character when running and moving the mouse too fast. When this would happen, I was unable to stop the camera from spinning with my mouse, and had to wait for it to slow down and stop. This could also be due to not having a proper camera controller script. I will need to revisit this and create a proper camera controller script. The second problem I was having was that over time, the character would rotate inside of controller, offsetting it's rotation from the camera in 3D space, which is also very problematic. Lastly, as we saw before, when "Apply Root Motion" was unchecked, I was getting problems with the sprinting animations looking dorky, possibly because they aren't blending properly.
In order to be able to check apply root motion, I had to do a few things, including deleting and re-importing most of my animations, with only the root translation zeroed out, will explain this later.
There is another reason why I spent a lot of time re-organizing my project. I have a lot of animations sitting on my computer, things I have purchased from places like the Unity Asset Store or Humble Bundle in the past, been drawing from many of those instead of just my MoCap. In order to go completely from MoCap, I would have to shoot a lot more MoCap, effective time management. My project had all of my animation libraries imported into it because I wanted to be able to preview them all. There were way too many animations, slowing things down, project is too big, can't move it from home to school computers. So, I separated them into 2 projects, one for previewing my animation libraries, the other for bringing chosen animations in and building functionality.
My animation preview layout:
There were way too many animations to just be sitting around in one Unity project unused:
I also worked on importing .fbx animations successfully from Blender. I found this video very helpful, from 7:35:
https://www.google.com/search?q=how+to+export+blender+animations+to+unity&rlz=1C1CHZL_enUS798US798&oq=how+to+export+blender+animations+&aqs=chrome.1.69i57j0l2.12989j0j7&sourceid=chrome&ie=UTF-8#kpvalbx=1
First thing to do is make sure all bones are selected in Pose Mode, not just the root bone or the armature object:
Next, these boxes need to be checked when exporting, all except "Force Start/End Keying."
For some reason, Blender doesn't like MoCap rigs or Vicon FBX format or something. Other rigs work, but when I try to export Vicon Blade MoCap animations/rigs from Blender to Unity, it breaks. I will need need to Use Motion Builder for those.
After that, when I have re-imported into Unity these check boxes need to be checked (all except Loop Time and Loop Pose):
The reason why these 3 check boxes need to be checked is to avoid the root rotation being applied to the character, which was causing him to rotate around in 3D space in the Unity engine.
So, sorta taken 2 steps forward, 1 step back, but it is for the better.
The reason why I deleted all of the animations from my project, zeroed out the root transforms, is so that I can check apply root motion, which fixes the problems I was having with blending between the running cycles, and also allows me to apply the root motion for some certain animations which will not be completely zeroed out on a single axis moving forward. This helps add the impression of motion to moving character so that it doesn't look like it's rolling in place while being translated on it's z axis. This is also good for me to know in case I need to use apply root motion in the future.
I also got jumping to work, but there are no animations applied, yet.
So the current state of my project is that I am still zeroing out root translation of the characters to the world origin. Once that is complete, they will be easy to add in because everything (state machines, animator, scripts, etc.) is still there.
Next week I will work more on fleshing out my character controller, adding more functionality. including falling and landing.
Monday, February 25, 2019
Blend Tree Movement and Aiming
Right now, I have movement working with a blend tree, blending between four running cycles, and four sprinting cycles. So, my character can run in eight directions by pressing any combination of one or two of the W, A, S, and D keys. If the Shift key is held, my character starts sprinting. Sprinting forward is still too explosive, I will need to slow that down by decreasing the value of the variable in the script. The rotation isn't that great, I will refine that when I go on to building my advanced 3rd-person character controller.
There is a problem, the character appears to rotate when moving around. I think this is caused by the rifle aiming animation, which plays randomly because I haven't set it up properly yet. I will have to zero out the transforms of that animation in the animation editor. Observe:
As you can see, the character rotates a bit to the right whenever the aiming animation loops, which, over time, causes the character to turn to the right like we saw earlier. I will also need to edit the position of the character's hands using Unity IK, because the rifle is not being gripped, and is being aimed too high.
Monday, January 14, 2019
Animation Issues
I made quick blockout animations in Blender for testing on my basic rig in Unity, but those bugged out. I have reason to believe it was due to the nature of Blender rigs. In Blender, the armature/bone system is very different than in Maya, and there isn't exactly a way to freeze transformations like there is in Maya. Even so, this is the first time I have had such an adverse reaction when importing a Blender rig into Unity.
I then resorted to Mixamo, and found that their animations and rigs do not work with Unity animation layers/avatar masks. I was still able to test out experimental movement where the player can sprint in all directions. I am reading Unity and Vicon Blade documentation. I don't want to attempt animation layers again unless I have usable MoCap data. Until then, I will focus on programming a FPS/3PS character controller.
Subscribe to:
Posts (Atom)
All Updated State Machines and Blend Trees
These are all of the State Machines and Blend Trees that I set up for my character in Mecanim:

-
In Unity, you can lock the state of the cursor while in play mode, effectively turning the cursor invisible and constraining it to the cente...
-
Re-targeting and editing animations was going pretty well. It was nice to revisit Motion Builder. Everything was going fine i...