- Home /
Question by
aslanulascan22 · Apr 07, 2020 at 08:46 AM ·
bool
How to use multiple booleons for an FPS Controller ?,How to control multiple booleons on a single script
I want to make an FPS game and I was trying to write a movement script using bools. it works when I use isGrounded with only one of other bools but when I use multiple bools like Crouch and Run Bools together they just dont want to work together. I dont know why and I really want to learn why they are not working. Here is my code:
public class RBController : MonoBehaviour
{
float moveForward;
float moveSide;
private float speed;
public float crouchSpeed;
public float slideSpeed;
public float runSpeed;
public float defaultSpeed;
public float JumpSpeed;
Rigidbody rig;
CapsuleCollider PlayerCol;
float originalHeight;
public float reducedHeight;
[SerializeField]
bool isGrounded;
[SerializeField]
bool isWalking;
[SerializeField]
bool isRunning;
[SerializeField]
bool isCrouching;
[SerializeField]
bool isSliding;
// Start is called before the first frame update
void Start()
{
rig = GetComponent<Rigidbody>();
speed = defaultSpeed;
originalHeight = PlayerCol.height;
}
// Update is called once per frame
void Update()
{
moveForward = Input.GetAxis("Vertical") * speed;
moveSide = Input.GetAxis("Horizontal") * speed;
rig.velocity = (transform.forward * moveForward) + (transform.right * moveSide) + (transform.up * rig.velocity.y);
//Sprint
if (Input.GetKey(KeyCode.LeftShift) && isGrounded)
{
isRunning = true;
}
else
{
isRunning = false;
}
if (isRunning)
{
Run();
}
else if (!isRunning)
{
Walk();
}
//Crouch
if (Input.GetKey(KeyCode.LeftControl))
{
isCrouching = true;
}
else
{
isCrouching = false;
}
if (isCrouching)
{
Crouch();
}
else if (!isCrouching)
{
GoUp();
}
}
void Run()
{
speed += runSpeed;
isCrouching = false;
}
void Walk()
{
speed = defaultSpeed;
}
void Crouch()
{
speed = crouchSpeed;
PlayerCol.height = reducedHeight;
}
void GoUp()
{
isCrouching = false;
speed = defaultSpeed;
PlayerCol.height = originalHeight;
}
private void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
private void OnCollisionExit(Collision col)
{
if(col.gameObject.tag == "Ground")
{
isGrounded = false;
}
}
}
Comment