- Home /
If statement with holding key and bool active.
I have a character that has run and walk animations. I also have a bool for running and isWalking and use animator setbools. I have it set to activate running if isWalking and player is holding down R, but when I stop walking, it stops physical movement but not the animation for running, so how would I fix that. Code is below, thanks in advance.
using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float baseSpeed = 5f;
[SerializeField]
private float runningSpeed = 10f;
private float speed;
[SerializeField]
private float lookSensitivity = 3f;
private bool running;
public bool isWalking;
private Animator anim;
private PlayerMotor motor;
void Start ()
{
motor = GetComponent<PlayerMotor>();
anim = GetComponentInChildren<Animator> ();
running = false;
}
void Update ()
{
float _xMov = Input.GetAxis ("Horizontal");
float _zMov = Input.GetAxis ("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
if (_zMov > 0.1)
isWalking = true;
else
isWalking = false;
anim.SetFloat ("walk", _zMov);
anim.SetBool ("running", running);
if (Input.GetKey (KeyCode.R) && isWalking == true) {
running = true;
speed = runningSpeed;
} else {
running = false;
speed = baseSpeed;
}
motor.Move (_velocity);
float _yRot = Input.GetAxisRaw ("Mouse X");
Vector3 _rotation = new Vector3 (0f, _yRot, 0f) * lookSensitivity;
motor.Rotate (_rotation);
float _xRot = Input.GetAxisRaw ("Mouse Y");
float _cameraRotationX = _xRot * lookSensitivity;
motor.RotateCamera (_cameraRotationX);
if (Input.GetKey (KeyCode.R))
running = true;
}
void AnimationSpeed()
{
float animSpeed = 2;
anim.speed = animSpeed;
}
}
Answer by Hertex · Sep 18, 2017 at 10:21 PM
try this:
if(Input.GetKey (KeyCode.R)){ running = true; speed = runnigSpeed; } else { running = false; speed = baseSpeed; }
instead of:
if (Input.GetKey (KeyCode.R) && isWalking == true) { running = true; speed = runningSpeed; } else { running = false; speed = baseSpeed; }
and
if (Input.GetKey (KeyCode.R)) running = true;
it should work, you have the if statement for running twice and i guess that's the problem.
Good luck!
Never $$anonymous$$d, I started to mess around with some stuff, and I fixed it. I removed the bottom if statement, and then added in the isWalking bool again, and it worked. Thanks for your help!
Answer by GameC12 · Sep 19, 2017 at 12:01 AM
First off, I am an idiot for not removing the if statement at the bottom, I put it in the first time I tried, and then moved it, but must have not deleted it. Second off, it still didn't work. Update Code: using UnityEngine; [RequireComponent(typeof(PlayerMotor))] public class PlayerController : MonoBehaviour { [SerializeField] private float baseSpeed = 5f; [SerializeField] private float runningSpeed = 10f; private float speed; [SerializeField] private float lookSensitivity = 3f; private bool running; public bool isWalking; private Animator anim; private PlayerMotor motor; void Start () { motor = GetComponent<PlayerMotor>(); anim = GetComponentInChildren<Animator> (); running = false; } void Update () { float _xMov = Input.GetAxis ("Horizontal"); float _zMov = Input.GetAxis ("Vertical"); Vector3 _movHorizontal = transform.right * _xMov; Vector3 _movVertical = transform.forward * _zMov; Vector3 _velocity = (_movHorizontal + _movVertical) * speed; if (_zMov > 0.1) isWalking = true; else isWalking = false; anim.SetFloat ("walk", _zMov); anim.SetBool ("running", running); if (Input.GetKey (KeyCode.R)) { running = true; speed = runningSpeed; } else { running = false; speed = baseSpeed; } motor.Move (_velocity); float _yRot = Input.GetAxisRaw ("Mouse X"); Vector3 _rotation = new Vector3 (0f, _yRot, 0f) * lookSensitivity; motor.Rotate (_rotation); float _xRot = Input.GetAxisRaw ("Mouse Y"); float _cameraRotationX = _xRot * lookSensitivity; motor.RotateCamera (_cameraRotationX); } }
That should make it less confusing.
using UnityEngine;
[RequireComponent(typeof(Player$$anonymous$$otor))]
public class PlayerController : $$anonymous$$onoBehaviour
{
[SerializeField]
private float baseSpeed = 5f;
[SerializeField]
private float runningSpeed = 10f;
private float speed;
[SerializeField]
private float lookSensitivity = 3f;
private bool running;
public bool isWalking;
private Animator anim;
private Player$$anonymous$$otor motor;
void Start ()
{
motor = GetComponent<Player$$anonymous$$otor>();
anim = GetComponentInChildren<Animator> ();
running = false;
}
void Update ()
{
float _x$$anonymous$$ov = Input.GetAxis ("Horizontal");
float _z$$anonymous$$ov = Input.GetAxis ("Vertical");
Vector3 _movHorizontal = transform.right * _x$$anonymous$$ov;
Vector3 _movVertical = transform.forward * _z$$anonymous$$ov;
Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
if (_z$$anonymous$$ov > 0.1)
isWalking = true;
else
isWalking = false;
anim.SetFloat ("walk", _z$$anonymous$$ov);
anim.SetBool ("running", running);
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.R)) {
running = true;
speed = runningSpeed;
} else {
running = false;
speed = baseSpeed;
}
motor.$$anonymous$$ove (_velocity);
float _yRot = Input.GetAxisRaw ("$$anonymous$$ouse X");
Vector3 _rotation = new Vector3 (0f, _yRot, 0f) * lookSensitivity;
motor.Rotate (_rotation);
float _xRot = Input.GetAxisRaw ("$$anonymous$$ouse Y");
float _cameraRotationX = _xRot * lookSensitivity;
motor.RotateCamera (_cameraRotationX);
}
}
Answer by Mihaere · Sep 19, 2017 at 12:15 AM
Also, if you haven't already in the PlayerMotor script, you should normalize your velocity or the player will move faster when using both axis (_xMov, _zMov) at the same time. But that's only if you haven't done this in the PlayerMotor script.
Your answer
Follow this Question
Related Questions
C# Acessing animator on another Object 1 Answer
Animator if Bool 1 Answer
Animator conditions, no bool state selector. 1 Answer
Animtor Stuck 1 Answer
Animator Bool: How do you use it? 1 Answer