While a lot of FPS games typically use a sprint/Left Shift to increase character speed, I am wanting to go with the Goldeneye/Perfect Dark approach where strafing while moving forward/backward gradually increases the speed.
Problem is, with my current code, I can’t seem to find a good way to detect whether if the player is moving diagonally or not, outside of using “Input.GetAxis(-axis-) !=0”.
I’ve also had to make two functions for increasing/decreasing speed, which seems kind of hacky.
Would rather be able to do something like “If W/UpArrow !=0 && A/Left Arrow !=0”, which may cause issues in regards to keybinds.
Is there any way I can clean up my code and are there alternate ways of detecting whether player is moving diagonal?
/
private void Update()
{
movement = new Vector3 (Input.GetAxis ("Horizontal"), 0f, Input.GetAxis ("Vertical"));
playSound = GetComponent<AudioSource> ();
if (Input.GetKey (KeyCode.LeftControl)) {
Crouch ();
} else {
Stand();
}
UpStep.transform.position = new Vector3(UpStep.transform.position.x, stepHeight, UpStep.transform.position.z);
if(Input.GetAxis("Horizontal") !=0 && Input.GetAxis("Vertical") !=0)
{
rareStrafe();
}
else
{
decreaseStrafe();
}
}
private void FixedUpdate(){
Move();
// This function is for a part in code regarding stairs
DetectSteps();
}
private void Move()
{
Vector3 MoveVector = transform.TransformDirection (movement) * moveSpeed;
rb.velocity = new Vector3 (MoveVector.x, rb.velocity.y, MoveVector.z);
if (Input.GetAxis("Vertical") !=0 || Input.GetAxis("Horizontal") !=0)
{
anim.SetBool ("walk", true);
}
else {
anim.SetBool ("walk", false);
}
}
private void rareStrafe()
{
// Gradually increase speed
moveSpeed += accel * Time.deltaTime;
if(moveSpeed > 4)
{
moveSpeed = 4.0f;
}
}
private void decreaseStrafe()
{
//Decrease the speed
moveSpeed -= deAccel * Time.deltaTime;
if(moveSpeed < 2f)
{
moveSpeed = 2;
}
}