Going off one of the mecanim examples from Unity (the one with the minebot) I am trying to make an Animator statemachine for a top down shooter with WASD controls (think Angry Bots)
Currently I have 2 states in the Animator, and Idle and a Moving, with the Moving being a blendtree (see image)
The blendtree is controlled by a float called Direction which is based on the inputaxes
float h = Input.GetAxis("Horizontal"); // setup h variable as our horizontal input axis
float v = Input.GetAxis("Vertical"); // setup v variables as our vertical input axis
anim.SetFloat("Direction", Mathf.Atan2(h,v) * 180.0f / Mathf.PI);
anim.SetFloat("Speed", h*h+v*v);
The problem is that based on this setup, Runforward is triggered at direction = 0 - which is the forward axis, but also true when there is no input. Normally this blendtree should transfer to the idle state based on Speed < 0.1, but for a split second when you release another key (say D for strafing right) it briefly tries to play RunForward as the Direction hits 0, before the Speed can get < 0.1 to change to idle.
The only transition I don’t have a problem with is obviously, RunForward (because direction is already set at 0)
I’m not sure how to get around this, but ideally I would not want to have to adjust the animations. They work perfectly fine with the AngryBots code.
Bump - honestly I thought mecanim would be an easier system than this. The example scenes show some interesting stuff, but most of them aren’t how characters move in most games and are very awkward to control. The Minebot example is probably the closest but it has this problem (not as noticible on the minebot than a biped) Maybe I’m setting up my Animator wrong? Should I have 3-4 states instead of 1 for walking? giving Run forward , run back , and strafe(x2) each their own state?
I’m dealing with this exact issue as well right now. I believe I know what the problem is but I don;t know how to fix it.
The following direction equation used in the minebot script is the source of the problem…I think:
avatar.SetFloat(“Direction”, Mathf.Atan2(h,v) * 180.0f / 3.14159f);
When you transition from left strafe to right strafe, there is a terrible glitch or blip in the animation. It does not transition smoothly at all if you arrow up to down or right to left and visa versa. However it works fine if you got up to right to down to left back to up in a circular sequence.
I believe the problem is that the equation does not smoothly transition from -90 to 90, for example as you go left to right. Instead it makes an abrupt full switch from -90 to 90. It only works if you are switching axis from vertical to horizontal but not horizontal to horizontal or vert to vert.
It appears that for a humanoid character, we need a different equation but I don’t know what it is yet.
I’ve been searching the asset store and as many mecanim examples as I can find but I have yet to find a Animator Controller and script that uses strafe left/right blended together with Walk forward and backward. It’s so strange because that’s the most basic control setup but the Mecanim demos and tutorial examples all somehow avoid a basic FPS or third person controller setup with no strafing. Instead they give examples of a guy running with a weird tight turn that no one would ever use in a real game.
Well let me know if you find a solution but I think the problem is the scripting not the blending.
I fixed the issue I was describing. It turns out the answer was built into the animator.setfloat overloads.
You can declare an amount of time for the float to reach the float value. If you leave that out, then it just jumps to the end result.
Use this:
animator.SetFloat(“Direction”, Mathf.Atan2(h,v) * 180.0f / 3.14159f, DirectionDampTime, Time.deltaTime);
Instead of this:
animator.SetFloat(“Direction”, Mathf.Atan2(h,v) * 180.0f / 3.14159f);
Notice I added a DirectionDampTime which I’ve seen in other Mecanim control scripts. I just didn’t bother to read about it.
I have DirectionDampTime = .25f; at the top of the class. This tells the animation to take .25 seconds to go from -90 to 90 or -180 to 180 thus allowing time for the animation to run as you transition from stafe left to right etc.
So have you guys made it play strafing correctly?
I notice that even with damping the character makes a run forward after the starfing ends because running forward is the default animation between strafing left and right.
How do you make correct strafing with WASD in Mecanim?
My best guess is Mecanim can’t actually do correct WASD motion in it’s current version because it can only blend one axis (either Horizontal or Vertical) but not both in a blend tree. According to the last Ninja Camp, they showed a video of them working on Blend Trees that allow 2 variables or 2 axis for blending. Until that happens, I’m not sure you can even achieve the Bootcamp demo character WASD motion. I could be wrong but I have not seen any demo or tutorial using Mecanim that replicates what is provided in the Bootcamp demo (proper WASD motion). I also find it a little odd that in the Mecanim demos and tutorials they do provide, the character movement is some weird variation that you would never use in a game. They use one state for backwards and a seperate state/blend tree for forwards. The forwards blend has blending for running right, straight and left forwards. But no strafing.
And that’s because if you try to combine strafing on the horizontal axis with forwards and backwards on the vertical axis in the same blend tree, the blend system has to pass through the strafe horizontal states in order to get to the vertical states and you see glitches in the animation.
That’s my best guess. I’d love it if someone would provide a controller and the code for input using mecanim that does basic WASD motion as seen in the Bootcamp demo.
As you said chaneya, the examples provided are really unusable, how weird that they didn’t release proper WASD controls in examples.
I’m not progressing any in my game because for a week I’ve been trying to fix this, so I’ll keep looking but for now I’m going to give it up. I hope when I come back to try it again I’ll be able to better understand it.
For now I’ll just make sure to prepare animations for my characters to work for mecanim.
I have a blend tree that works good for movement anywhere between East North and West (havent gotten to southern directions yet). I form a diamond with a cross in the middle.
Top tip: run forward
Bottom tip: idle
Left point: blend tree of run forward and strafe left
Right point: vice versa
blend trees on the side points blend to a 100% side strafe when forwards=0 and 100% forward run when forward=1
the left and right trees can transition directly to each other based on the side variable, but cannot transition to the top tip but they can be reached from the top tip. This way we stay in the left or right blend trees as much as possible to minimize transitions and we can always transition directly to the state we want.
*** for spacing
****************Run
*****/|* (on this line / and \ only go down)
L_blend tree<------>R_blend tree
*************|/
****************idle
My problem right now is scripting the control inputs so that I get
vars forwards=1, and side=0 on uparrow,
vice versa on side arrow, 50/50 on both up and side arrow
and all the in between.
I thought I would go about this with a nested locomotion blend tree, which blends between 2 sub blend trees, one for each axis. The horizontal tree would blend based on the input in the range -1 - 1 and go between the left strafe, still/idle, and right strafe, correspondingly. The vertical tree would do the same for back, still, and forwards animations.
The parent blend tree would blend between these based on the ratio of horizontal to vertical input - abs(h)/(abs(h)+abs(v)).
Am I missing something? The input script would need to go something like:
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
// possibly do some deadzone code here if both h and v are near zero
anim.SetFloat("Horizontal", h);
anim.SetFloat("Vertical", v);
float ah = Mathf.Abs(h);
float av = Mathf.Abs(v);
if (ah+av != 0.0f)
anim.SetFloat("Direction", ah/(ah+av));
else
anim.SetFloat("Direction", 0.0f);
I’ve been struggling with this as well, I’m trying to link blend trees together and using additional “smoothing” directional values, it’s close to working well - demo here: http://logicwell.com/nightfall/tpc.html - hit 1 to enter combat mode and you can look down to observe legs while changing directions. Definitely a ways off!
My new tree is like duencil’s I think.
I have a blend tree for forwards/back that blends between 3 blend trees of
runforward with strafe,
no forward motion and strafe,
and backwards with strafe.
I blend my inputs with Vector3(Input Vertical, 0, Input horizontal).normailzed and then pull out the x and y components
The biggest problem is when not moving forwards any and I go from left to right strafe theres a twitch because I cant get it to skip transitioning out of my trees and through idle
Tried your player Lypheus, yours seems to work great, its pretty hard to get the legs to go through each other and I wouldnt trouble over it anymore myself since the bootcamp demo and guy fighting a drone demo dont do any better. But your guy seems to stomp about like a robot
Yea, this thread is older than the update that gave 2d cartesian trees.
Thanks for the link, glad to hear it works good.
I’ve been putting off updating my project for fear it would not work. I’ll probly use the freeform directional.
Well, Strafe + Walk blending usually ends up like this. Note, that we have 3 parameters: Speed [for walking -1…+1] as Speed multiplier, “BTSpeed” in my case - is the speed clamped from 1 to +inf, in order, if speed is 0, the Idle animation to still play, and, obviously, Strafe, from -1 to 1 with speed multiplier. Of course, you have to play a bit with this setup.