So I’ve been trying to make a wall jump for my game for a while now, and nearly found a solution. With this script, you can detect when a wall is near you, where the wall is and when you press the space bar It shoots you up and the direction the wall is facing. The problem is that when you actually do wall jump it’s almost instant as if you teleported. Any suggestions or possible fixes? Any answers are appreciated.
Movement Script:
void FixedUpdate()
{
moving = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D);
if (moving)
{
transform.localRotation = Quaternion.Euler(0, Camera.rotation.eulerAngles.y, 0);
}
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
Vector3 move = (transform.right * x + transform.forward * y);
move.Normalize();
rb.velocity = new Vector3(move.x * speed * Time.deltaTime, rb.velocity.y, move.z * speed * Time.deltaTime);
onGround = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), spSize, Environment);
}
Wall Check:
private void CheckForWall() // called every update
{
isWallRight = Physics.Raycast(transform.position, transform.right, 0.73f, Environment);
if (isWallRight)
{
wallJump = -transform.right * jumpForce;
canWallJump = true;
Debug.Log("Right Wall");
}
isWallLeft = Physics.Raycast(transform.position, -transform.right, 0.75f, Environment);
if (isWallLeft)
{
wallJump = transform.right * jumpForce;
canWallJump = true;
Debug.Log("Left Wall");
}
if (!isWallLeft && !isWallRight)
{
canWallJump = false;
}
}
And finally the wall jump:
private void WallJump()
{
rb.AddForce(jump * 15, ForceMode.Impulse);
rb.velocity = Vector3.zero;
rb.AddForce(wallJump * wallJumpForce, ForceMode.VelocityChange);
speed = jumpSpeed;
void Update()
{
rb.AddForce(0, -gravity * Time.deltaTime, 0);
CheckForWall();
if (Input.GetKeyDown(KeyCode.Space) && onGround == true)
{
speed = jumpSpeed;
onGround = false;
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
}
if (Input.GetKeyDown(KeyCode.Space) && canWallJump && moving)
{
WallJump();
}
}
}