Im trying to get my character to start running with the run animation and at a faster speed and I hgave tried many ways but cant get it to work. I already have the run animation set in the animator as “isRun”.
I can’t confirm that your animations are set up right, but the way you have coded the above, you have a lot of redundant coding. There is a phrase in programming - Don’t Repeat Yourself (DRY).
Because running should override walking, put the running check first, and you may want the player holding W while running too.
Lastly, just my own suggestion, read up on the input manager. It will allow you to set up controls that work across multiple platforms or controllers.
Also, I typed the above due to lazyness and lack of time, sorry. You wouldn’t want to repeatedly set these bools the entire time the key is held down. Just need to do it once, and when you release the keys, you can unset if necessary. That will require some changes to the logic, so I hope you can figure out how that should work. You can use GetKeyDown for doing something when the key is initially pressed down, and GetKeyUp for when the key is released. Using GetKey should work though, but it is probably causing some unnecessary processing though.
Just some pseudo code…
if getkeydown left shift, and getkey (no reference down/up) W… run animation
else if getkeydown w/a/s/d… walk animation…
else if getkey w/a/s/d … do nothing… (this is just so that it doesn’t fall into the idle animation)
else idle
If you only want to click left shift to toggle run, then you can use GetKeyDown or GetKeyUp (if you don’t have to keep it held down for running).
As for your second question, you’d have to adjust the straffe and translation after you’ve determined if you’re running and adjust accordingly.
I got him moving faster. But I dont think GetKeyDown is what I want. The character needs to start running while hes walking and when your click LeftShift he goes from walking to Running, currently when this happens the speed changes correctly but the animation doesnt switch.
Here is my updated code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterControl : MonoBehaviour
{
private float speed = 0;
public float wSpeed;
public float rSpeed;
static Animator anim;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
anim = GetComponent<Animator>();
}
void Update()
{
float z = Input.GetAxis("Vertical") * speed;
float y = Input.GetAxis("Horizontal") * speed;
transform.Translate(0, 0, z);
transform.Translate(y, 0, 0);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
if (Input.GetKey(KeyCode.W) && (Input.GetKey("left shift")))
{
speed = rSpeed;
anim.SetBool("isWalk", false);
anim.SetBool("isIdle", false);
anim.SetBool("isRun", true);
}
else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A)))
{
speed = wSpeed;
anim.SetBool("isWalk", true);
anim.SetBool("isIdle", false);
anim.SetBool("isRun", false);
}
//Idle
else
{
anim.SetBool("isWalk", false);
anim.SetBool("isIdle", true);
anim.SetBool("isRun", false);
}
}
}
Another solution for your question on how to make him move faster, move the translation and strffe to the end of block, and modify the speed depending on the isRunning flag. for example you can make it:
I think that your best is to do sort of a flowchart (man, one of my old instructors would be proud… this may be a first I’d recommended it, however) and just figure out the different scenarios that lead to walk, run, and idle. Then structure your if/else if/else conditions to cover all these different branches.
Just a quick primer, in case you don’t already know:
GetKeyDown = when the user first presses the key
GetKey = the user is holding the key down currently
GetKeyUp = the user just released the key.
It’s best you prioritize your if/else if/else branches so that you can code these conditions without over-convoluting things and having a minimal amount of branches… in this case, our 3rd branch is a catch all, if I did this right… perfect.
Run:
This happens if W is held and left shift IS held… (If GetKey(KeyCode.W) && GetKey(“left shift”))
Walk:
This happens if W is held and left shift is NOT held… (If GetKey(KeyCode.W) && !GetKey(“left shift”))
Idle:
This is just your else. Every other situation this should be called.
Now that should be the quick/dirty approach. This would repeatedly set these bools every frame though, and that is likely inefficient, especially since you are referring to the animations by string name (we’ll talk about that later when you have a handle on this main issue).
Once you get that working right, I’d move on to the next step here, and try and get this to work for where you only have to set your animation parameters on a state change (ex. going from running to walking). That might be an “optimization” that maybe isn’t even worth doing… I don’t know. But, that will either involve using some combination of GetKeyDown and GetKeyUp checks to set the parameters only when you need to… or you build some variables for the states. Compare the state that the current frame should be to what it was in the previous frame. You can determine the state of the current frame simply enough by what I already mentioned above. If the state changes, then OK, you need to set your parameters … otherwise… do nothing.
Okay I figured it out, I had to make transitions between my Walk and Run animations, smh. I’m stupid for not realizing that. But I don have another question, I noticed when I run my players head moves down a little which I want to happen but my camera which is child of my character still stays in the same spot. How do I make the camera move with my characters head, especially later when my character is crouching an such?
Can you attach the camera to the head, then just offset it’s position (i.e. change it’s transform so that it’s at the correct height you want, relative to the head)? that’s assuming your head is actually a child game object of your player. Not sure how your model is set up. In models I’ve been using, all the body parts are separate game objects. I haven’t used Unity long enough to know if that’s how it always is or if that’s uncommon, sorry.
Really, if your head is rotating (even if it’s just the body that rotates, and the head follows along… if you get my meaning), I would expect your camera to be doing the exact same thing…
And your player’s body doesn’t rotate at all? Can you describe your game a bit… how movement works… is it an FPS… or 3rd person, for example… is there a particular reason why you want your player not rotating, but you want the camera to rotate, etc…
If your player’s body never rotates but you want the camera too, then you’ll probably have to “disconnect” the Y rotation (assuming that’s the right axis) and base this on your controller input somehow. That might be a little bit of work to figure out.
Ok its an FPS, when W and S are pressed he walks/runs forward and back and when A and D are pressed he straffes left and right, doesnt rotate (thats how I want it). Before I put the camera on the head it was child to the character and using my mouse I could look in any rotation up/down/left/right and I used that for the direction my character moves. But now that the camera IS on the head when I use the mouse to look I am only able to look up and down. Hopefully that makes sense.
Ohhh, I see. So really the problem you’re having is that your mouse look no longer works fully. The funny thing is, I’m not sure how having it as a child of the head (that is what you did, right?) instead of a child of the whole game object would make a difference to your mouse look.
Might have to see if someone else pops into this discussion to see if there’s something that jumps out in your camMouseLook function. If you didn’t change this at all, I would have expected it to work as intended just by moving the camera from being a child of the character to a child of the head. Can you take a screenshot of your hierarchy just showing your character all expanded out so I can see how that’s structured?