- Home /
How do you add strafe animations to a character?
I know this sounds dumb but i have made all the animations for my character. The last things are the strafe animations (side to side animations when they are only pressing "A" or "D" on the keypad)
The problem is, I don't know where i would add these animations in the character motor. I have a different character motor than originally because i found one that offers more options.
Because im not sure on how to do this exactly in mechanim, i was just going to call the animations using JS when the player pushes one of those buttons. I just dont know when in the script it is called and what to put when it is called.
Help much appreciated here is my function fixedupdate and some of my function start I tried to include as little as possible
function Start () {
controller = GetComponent(CharacterController);
myTransform = transform;
speed = walkSpeed;
rayDistance = controller.height * .5 + controller.radius;
slideLimit = controller.slopeLimit - .1;
jumpTimer = antiBunnyHopFactor;
oldPos = transform.position;
}
function FixedUpdate() {
var inputX = Input.GetAxis("Horizontal");
var inputY = Input.GetAxis("Vertical");
// If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed
var inputModifyFactor = (inputX != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
if (grounded) {
var sliding = false;
// See if surface immediately below should be slid down. We use this normally rather than a ControllerColliderHit point,
// because that interferes with step climbing amongst other annoyances
if (Physics.Raycast(myTransform.position, -Vector3.up, hit, rayDistance)) {
if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
sliding = true;
}
// However, just raycasting straight down from the center can fail when on steep slopes
// So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead
else {
Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, hit);
if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
sliding = true;
}
// If we were falling, and we fell a vertical distance greater than the threshold, run a falling damage routine
if (falling) {
falling = false;
if (myTransform.position.y < fallStartLevel - fallingDamageThreshold && enableFallingDamage == true)
ApplyFallingDamage (fallStartLevel - myTransform.position.y);
}
// If running is enabled, change to run speed when left shift is pressed:
if (Input.GetKey(KeyCode.LeftShift) && enableRun == true)
{
speed = runSpeed;
}
// If crouching is enabled, change to crouch speed when "c" is pressed:
else if (Input.GetKey("c") && enableCrouch == true)
{
speed = crouchSpeed;
}
// If nothing is pressed, use walkSpeed:
else
{
speed = walkSpeed;
}
Answer by kacyesp · Aug 28, 2014 at 02:45 AM
Open up the animator (Window -> Animator).
Click on your character in the hierarchy you wish to add animations to.
Now drag your animations into the animator (if they're not already there).
Add transitions between the animations you want to be able to switch from and to (you do this by right clicking on an animation in the animator and you'll see an option to make a transition).
Add a float parameter (bottom left corner of animator window). Call it speed or whatever you want.
Click on the transition arrows you made in step 4 and set the condition to be based on the speed speed parameter you just made. For example, if the speed is greater than some number, you'll want to strafe to the right.
Create an instance variable ( aka field ) of type Animator. In your Start function add:
animator = GetComponent();
Finally, at the bottom of FixedUpdate, add:
animator.SetFloat( "speed", speed ); //replace the string with whatever you named the parameter in the animator
Believe it or not i have already done this with the walk and run animation. I named it exactly "Speed" I assume this means i should create a new float parameter named "SideSpeed" or something. What im not sure of however is where to say "not sure of function" = "SideSpeed" Currently i am calling normal speed from a different script (it would make more sense to call them both from the same script, but i thought it would be easier to figure it out in the character motor) Here is the current "Speed" calling i have.
void FixedUpdate ()
{
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("Speed", v); // set our animator's float parameter 'Speed' equal to the vertical input axis
//anim.SetFloat("Direction", h); // set our animator's float parameter 'Direction' equal to the horizontal input axis
anim.speed = animSpeed; // set the speed of our animator to the public variable 'animSpeed'
anim.SetLookAtWeight(lookWeight); // set the Look At Weight - amount to use look at I$$anonymous$$ vs using the head's animation
currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
if(anim.layerCount ==2)
layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1); // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
Would i create a new function or something? What deter$$anonymous$$es the side movement?
Its commented out because i was messing around on a different script.
I think I ran into a similar issue yesterday of "Where do I update animations?" $$anonymous$$y solution was to have the motors/controllers update a PlayerState class/struct and then make an AnimationController to update the flags for the animator based on the values set in the same PlayerState. When you know the player is strafing, then set an IsStrafing boolean to true in the character motor. Then the AnimationController will update the Animator's parameters. Does that answer your question?
So you think i should create like a whole new script for animations and say something like "if(Input.GetkeyDown(keycode.D))set like a parameter thats 1 through -1 and say -1 strafes to the left and 1 strafes to the right? I know you said boolean but im not 100% sure i would know how to do it with a boolean.
Haha thanks for the great idea! I will try it out today! Thanks!
Your answer
Follow this Question
Related Questions
How to open a door with the "E" key 1 Answer
How would you reccommend adding the jumping animation to my script? 0 Answers
Texture Swap + Animated Offset/Tiling 1 Answer
Rotating An Object On Its Axis Once 3 Answers
Animation play when dead 2 Answers