These are all of the State Machines and Blend Trees that I set up for my character in Mecanim:
Saturday, March 23, 2019
All Updated Scripts--Remaining Scripts
This is the script that controls the rotation of the camera around my character:
Lastly, this script controls the muzzle flash on my character's weapon, nothing fancy:
Lastly, this script controls the muzzle flash on my character's weapon, nothing fancy:
All Updated Scripts--Movement Controller
This is the script that controls all of my character's movement in 3D space, translation, rotation, and forces being applied:
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
Project Roadmap
Well, here is what was completed, and what was not. Accomplishing all of these tasks in such a short time frame was overzealous anyway. I will take the concepts I learned from this into future game projects. Unity Mecanim is very powerful for creating character controllers and correcting animations. Utilizing it will be a great asset if I follow through with it.
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.
We can also see that turning animations play on the lower half of the body if the character is turning. This acts strangely if the character is moving around. If I keep this feature, then I will script the necessary lines to prevent that. See the extra layer added for lower body, and it's corresponding weight.
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:
Scripts: Cursor Lock State
I am now posting all of the scripts that I created for this project.
This one is almost entirely from Unity documentation, except that I modified it.
This one is almost entirely from Unity documentation, except that I modified it.
CursorLockState, simply just allows the cursor to be locked (constrained to center of screen, and turned invisible) and unlocked:
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:
-
Re-targeting and editing animations was going pretty well. It was nice to revisit Motion Builder. Everything was going fine i...
-
Controlling entire spine with Unity IK, may be useful in the future: https://www.youtube.com/watch?v=gsVZOpMia7w