Can't Jump While Wall Running
I am coding a character movement scripts and I'm trying to code wall running. He is able to run on the wall but I'm trying to get the player to jump between walls.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallRunning : MonoBehaviour
{
[Header("Wallrunning")]
[SerializeField] LayerMask whatIsRunAble;
[SerializeField] LayerMask whatIsGround;
[SerializeField] float wallRunForce;
[SerializeField] float wallRunUpForce;
[SerializeField] float wallRunSideForce;
[SerializeField] float wallClimbSpeed;
[SerializeField] float MaxWallRunTime;
float wallRunTimer;
[Header("Input")]
KeyCode jumpKey = KeyCode.N;
KeyCode upwardsRunKey = KeyCode.CapsLock;
KeyCode downwardsRunKey = KeyCode.B;
bool upwardsRun;
bool downwardsRun;
float horizontalInput;
float verticalInput;
[Header("Detection")]
[SerializeField] float wallCheckDistance;
[SerializeField] float minJumpHeight;
RaycastHit leftWallHit;
RaycastHit rightWallHit;
bool wallLeft;
bool wallRight;
[Header("Exiting")]
[SerializeField] float exitWallTime;
bool exitingWall;
float exitWallTimer;
bool jumping;
[Header("References")]
[SerializeField] Transform orientation;
MoveChar mc;
Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
mc = GetComponent<MoveChar>();
}
// Update is called once per frame
void Update()
{
CheckForWall();
StateMachine();
}
private void FixedUpdate()
{
if (mc.wallRunning)
{
WallRunMove();
}
}
void CheckForWall()
{
wallRight = Physics.Raycast(transform.position, orientation.right, out rightWallHit, wallCheckDistance, whatIsRunAble);
wallLeft = Physics.Raycast(transform.position, -orientation.right, out leftWallHit, wallCheckDistance, whatIsRunAble);
}
bool AboveGround()
{
return !Physics.Raycast(transform.position, Vector3.down, minJumpHeight, whatIsGround);
}
void StateMachine()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
THIS IS WHERE THE ERROR IS (I THINK) AND IT HAS TO DO WITH THE exitingWall BOOL
if ((wallLeft || wallRight) && verticalInput > 0 && AboveGround() && !exitingWall)
{
/*if (Input.GetKeyDown(jumpKey))
{
WallJump();
}*/
if (!mc.wallRunning)
{
StartWallRun();
}
}
else if (exitingWall)
{
if (mc.wallRunning)
{
StopWallRun();
}
if (exitWallTimer > 0)
{
exitWallTimer -= Time.deltaTime;
}
if (exitWallTimer <= 0)
{
exitingWall = false;
}
}
else
{
if (mc.wallRunning)
{
StopWallRun();
}
}
}
void StartWallRun()
{
mc.wallRunning = true;
}
void WallRunMove()
{
rb.useGravity = false;
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;
Vector3 wallForward = Vector3.Cross(wallNormal, transform.up);
if ((orientation.forward - wallForward).magnitude > (orientation.forward - -wallForward).magnitude)
{
wallForward = -wallForward;
}
rb.AddForce(wallForward * wallRunForce, ForceMode.Force);
if (Input.GetKey(upwardsRunKey))
{
rb.velocity = new Vector3(rb.velocity.x, wallClimbSpeed, rb.velocity.z);
}
if (Input.GetKey(downwardsRunKey))
{
rb.velocity = new Vector3(rb.velocity.x, -wallClimbSpeed, rb.velocity.z);
}
if (!(wallLeft && horizontalInput > 0) && !(wallRight && horizontalInput < 0))
{
rb.AddForce(-wallNormal * 100, ForceMode.Force);
}
}
void StopWallRun()
{
mc.wallRunning = false;
rb.useGravity = true;
}
void WallJump()
{
exitingWall = true;
exitWallTimer = exitWallTime;
Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;
Vector3 forceToApply = transform.up * wallRunUpForce + wallNormal * wallRunSideForce;
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
rb.AddForce(forceToApply, ForceMode.Impulse);
}
}
Thanks for any help in advance. I've been stuck on this for about 5 hours now (probably should've come here sooner). I wrote in capitols in the code where I think the problem is. It seems to work without the bool but is also leading to other errors as I need to stop the StartWallRun for a small period of time (so far about 0.2 seconds) so I don't get attached back to the wall. Thanks
Your answer
Follow this Question
Related Questions
Double Jump using character controller 0 Answers
my player can jump in mid air,my charater can jump in mid air how do i fix? 0 Answers
Player keeps spinning after collision 0 Answers
Why does my player weirdly clip into environment when jumping? 0 Answers
Make the Player unable to move to opposite direction 1 Answer