Two IF Statements not working in one method
I'm making my own first person controller and I want to have 3 movement speeds: walk, run and sprint. I've got my character to walk and sprint, but not running. When i flip the two if statements in the ChangeMovementSpeed() method, this time walking and running works but not sprinting. I'm not exactly new to C# (around 6 months) but it must be my stupidity and lack of experience that's causing this problem. Any help is appreciated. Thanks.
My First Person Controller code:
Note: I'm using Naughty Attributes for organization purposes :)
using System;
using UnityEngine;
using NaughtyAttributes;
namespace ZGS
{
[RequireComponent(typeof(CharacterController))]
public class ZFirstPersonController : MonoBehaviour
{
#region Variables
#region Settings
#region Public
[BoxGroup("Settings")] public float walkSpeed;
[BoxGroup("Settings")] public float runSpeed;
[BoxGroup("Settings")] public float sprintSpeed;
[BoxGroup("Settings")] public float jumpHeight;
[BoxGroup("Settings")] public float gravity = -9.81f;
[BoxGroup("Settings")] public CharacterController characterController;
#endregion
#region Private
[BoxGroup("Settings")] private float movementSpeed;
[BoxGroup("Settings")] Vector3 velocity;
#endregion
#endregion
#region Ground Check
#region Public
[BoxGroup("Ground Check")] public float groundDistance;
[BoxGroup("Ground Check")] public LayerMask groundMask;
[BoxGroup("Ground Check")] public Transform player;
#endregion
#region Private
[BoxGroup("Ground Check")] bool isGrounded;
#endregion
#endregion
#endregion
#region Standard Methods
void Update()
{
ExecuteGroundCheck();
ChangeMovementSpeed();
ExecuteMovement();
ExecuteJump();
ExecuteGravity();
}
#endregion
#region Custom Methods
void ExecuteGroundCheck()
{
isGrounded = Physics.CheckSphere(player.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
}
void ChangeMovementSpeed()
{
if (Input.GetKey(KeyCode.LeftShift))
{
movementSpeed = runSpeed;
}
else movementSpeed = walkSpeed;
if (Input.GetKey(KeyCode.LeftAlt))
{
movementSpeed = sprintSpeed;
}
else movementSpeed = walkSpeed;
}
void ExecuteMovement()
{
float x = Input.GetAxis("Move");
float z = Input.GetAxis("Strafe");
Vector3 move = transform.right * x + transform.forward * z;
characterController.Move(move * movementSpeed * Time.deltaTime);
}
void ExecuteJump()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
void ExecuteGravity()
{
velocity.y += gravity * Time.deltaTime;
characterController.Move(velocity * Time.deltaTime);
}
#endregion
}
}
Answer by tormentoarmagedoom · Feb 29, 2020 at 01:13 AM
Hello.
Yes, its normal. your code you have do this every frame:
if X -> A
else > B
if Y >C
else B
So, every frame if X is pressed, A uis active, but the line 4 makes B active at the end, so your final result is B. If Y is pressed, line 2 makes B active, but then line 3 makes C active.
So you are doing "2 decicions" each fram, the decision Line 1 or Line 2 and the decision line 3 or line 4.
You need a code that onle does ONE decision with 3 different results, so you need this:
if (Input.GetKey(KeyCode.LeftShift))
{
movementSpeed = runSpeed;
}
else if (Input.GetKey(KeyCode.LeftAlt))
{
movementSpeed = sprintSpeed;
}
else movementSpeed = walkSpeed;
This way, only one of the 3 resultswill be the winner.
BYEE! !
Answer by abolfazlhosnian · Mar 01, 2020 at 01:13 PM
replace this function instead of yours:
void ChangeMovementSpeed()
{
if (Input.GetKey(KeyCode.LeftShift)&Input.GetKey(KeyCode.LeftAlt))
{
movementSpeed = walkSpeed;
}
else if (Input.GetKey(KeyCode.LeftShift))
{
movementSpeed = runSpeed;
}
else if (Input.GetKey(KeyCode.LeftAlt))
{
movementSpeed = sprintSpeed;
}
else
{
movementSpeed = walkSpeed;
}
}
Your answer
Follow this Question
Related Questions
C# If string is equal to any in array 2 Answers
CS1023 An embedded statement may not be a declaration or labeled statement help 1 Answer
if (transform.position.x == -38.12f) not working :\ 3 Answers
I am trying to close the GUI if its already active and the user clicks 2 Answers
If statement for direction the character is facing 0 Answers