Hello everyone,
I’m making my first game and I have an issue where my player sticks to wall if he’s pressing the forward key while looking at a wall mid air.
I understand that it comes from the fact that my controller adds force to the player when pressing the forward key, so I added a bool “is facing wall” that takes the value of a raycast in front of the player to check if he’s facing a wall, and if he is, do not apply any force. (I also understand that this will make the player unable to move when facing a wall but that will be an issue for later lmao)
However, even like this, it doesn’t work. Any idea? Here’s my PlayerController:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed;
public float groundDrag;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
public Vector3 CurrentForce;
bool readyToJump = true;
[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;
[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
[Header("Wall Check")]
public float minDistance=0.2F;
bool facingWall;
[Header("animation")]
public Animator anim;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void FixedUpdate()
{
MovePlayer();
}
private void Update()
{
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
anim.SetBool("grounded", grounded);
MyInput();
SpeedControl();
if(grounded)
{
rb.drag = groundDrag;
}
else
{
rb.drag = 0;
}
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
anim.SetFloat("vertical", verticalInput);
anim.SetFloat("horizontal", horizontalInput);
if(Input.GetKey(jumpKey) && readyToJump && grounded)
{
anim.SetTrigger("jump");
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
}
private void MovePlayer()
{
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
facingWall = Physics.Raycast(transform.position, moveDirection.normalized, minDistance);
if (!facingWall) {
if (grounded)
{
rb.AddForce(moveDirection.normalized * moveSpeed * 10F, ForceMode.Force);
}
else
{
rb.AddForce(moveDirection.normalized * moveSpeed * 10F * airMultiplier, ForceMode.Force);
}
}
}
private void SpeedControl()
{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
if (flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
CurrentForce = rb.velocity;
}
private void Jump()
{
rb.velocity = new Vector3(rb.velocity.x , 0f, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
anim.ResetTrigger("jump");
readyToJump = true;
}
}
Thank you for your help!