I was just finishing up a wall run script from a guide when all of a sudden, the wall run is making me run on air? It is effectively just making me float around, any ways I can make it not make me fly?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Wallrunning : MonoBehaviour
{
//Stating all of the things.
[Header("Wallrunning")]
public LayerMask whatIsWall;
public LayerMask whatIsGround;
public float wallRunForce;
public float wallJumpUpForce;
public float wallJumpSideForce;
public float maxWallRunTime;
private float wallRunTimer;
[Header("Input")]
private float horizontalInput;
private float verticalInput;
[Header("Detection")]
public KeyCode jumpkey = KeyCode.Space;
public float wallCheckDistance;
public float minJumpHeight;
private RaycastHit leftWallHit;
private RaycastHit rightWallHit;
private bool wallLeft;
private bool wallRight;
[Header("Exiting")]
private bool exitingWall;
public float exitWallTime;
private float exitWallTimer;
[Header("References")]
public Transform orientation;
private PlayerController pm;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
pm = GetComponent<PlayerController>();
}
private void Update()
{
CheckForWall();
StateMachine();
}
private void FixedUpdate()
{
if(pm.wallrunning)
WallRunningMovement();
}
private void CheckForWall()
{
wallRight = Physics.Raycast(transform.position, orientation.right, out rightWallHit, wallCheckDistance, whatIsWall);
wallLeft = Physics.Raycast(transform.position, -orientation.right, out leftWallHit, wallCheckDistance, whatIsWall);
}
//Raycast to check for wall is above and below this message.
private bool AboveGround()
{
return !Physics.Raycast(transform.position, Vector3.down, minJumpHeight, whatIsGround);
}
private void StateMachine()
{
//Inputs.
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
if ((wallLeft || wallRight) && verticalInput > 0 && AboveGround() && !exitingWall)
{
if (!pm.wallrunning)
StartWallRun();
if (Input.GetKeyDown(jumpkey)) WallJump();
}
else if (exitingWall)
{
if (pm.wallrunning)
StopWallRun();
if (exitWallTimer > 0)
exitWallTimer -= Time.deltaTime;
if (exitWallTimer < 0)
exitingWall = false;
}
// New state????
else
{
if (pm.wallrunning)
StopWallRun();
}
}
private void StartWallRun()
{
pm.wallrunning = true;
}
private void WallRunningMovement()
{
rb.useGravity = false;
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.y);
Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;
Vector3 wallForward = Vector3.Cross(wallNormal, transform.up);
//Adding force now.
rb.AddForce(wallForward * wallRunForce, ForceMode.Force);
if (!(wallLeft && horizontalInput > 0) && !(wallRight && horizontalInput < 0))
rb.AddForce(-wallNormal * 100, ForceMode.Force);
}
private void StopWallRun()
{
pm.wallrunning = false;
}
private void WallJump()
{
exitingWall = true;
exitWallTimer = exitWallTime;
Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;
Vector3 forceToApply = transform.up * wallJumpUpForce + wallNormal * wallJumpSideForce;
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.y);
rb.AddForce(forceToApply, ForceMode.Impulse);
}
}