Looking for some guidance on mechanim state machines and coding
Hello All,
This is my first post and I hope I have done it in the correct way!
I have been working on my first "game". Really its just a simple shooter, but mostly a means to help me learn and I have been trying to involve many aspects of unity for learning. I'm looking for some guidance on my player controller and how I have it setup and if I am..well..going down the correct path for animating and moving him. I know there are several ways to execute a task, and I have been going through a ton of tutorials.
I have him moving around the level, no problems there, just simple transform.Translate code.
My character is a simple flying mech that was made in Maya with animations of: Forward, back, left,right, front left, front right(etc) that give the effect of moving in that direction. What I am trying to do is smoothly transition between them using mechanim, when the player moves from left, to right, forward, etc....and then calling those code using the boolean parameters in a script.
What I have is as web of the 8 states in mechanim:
What I then have from code is something like this:
if(Input.GetKey(KeyCode.W))
{
animator.SetBool("move_forward",true);
}
else
{
animator.SetBool("move_forward",false);
}
For moving diagonal my code is:
if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
{
animator.SetBool("move_forward",false);
animator.SetBool("move_LF",true);
}
else
{
animator.SetBool("move_LF",false);
}
I know you can use the integer parameters and use the input axis in code, though I am not sure i want this as i already have the movement established elsewhere in code. I am also using DOTween code to create a strafe effect when the player pushes space bar and pointed in the player direction. Here is the code for that, if it is helpful:
if(Time.time > nextFire && Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.Space))
{
Debug.Log("Player Pushing W and A");
Debug.Log("Spacebar pushed, strafing FORWARD/ANGLE A");
Quaternion moveAng = Quaternion.Euler(0,-45,0);
destination = transform.position + moveAng *(Vector3.forward * strafeAngleSpeed);
transform.DOMove(destination,.2f).SetUpdate(true);
nextFire = coolDownPeriod + Time.time;
While this system is "working" and I still have to finish some transitions from states, I suppose what I am looking for is some guidance. Am I going down the correct path here? Do the mechanim states look like I am doing things correct for my situation, or should I be doing something more efficient? Am I correctly coding the transitions with booleans or can someone point me in a better direction? I have been seeing some issues when the player pushes both W & A, or S &D(or any 2 keys). It seems to trigger 9/10 times, and sometimes the state will automatically go to "move_forward", instead of move_RF or move_LF. I was able to correct some of this using the interruption source moving from idle to a diagonal direction.
This is my first real project after doing a lot of tutorial projects, and kind of my first intro into bringing my own rigged and animated character into Maya(which is very cool to do, but still very new at it :) ); so any kind of guidance is much appreciated. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612092223im_/https://answers.unity.com/themes/thub/images/avi.jpg)