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;
	}
}

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!

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);
}

}`

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.