Hello,
I marked this as a physics issue, but I’m not positive that’s where the problem lies. Please forgive me if this is posted in the wrong place.
I’m running into a strange bug when play testing my player movement script. Currently, the player can run right/left, jump, and when the player jumps into a wall, they enter a “wall slide” where their vertical velocity is reduced to -1 so they very slowly slide down the wall until they either A) hit the jump button, resulting in a vault away from the wall, or B) hit the “s” key, which exits the wall slide. However, if there is a wall floating in the air, and the player slides all the way to the bottom of the wall, right as the top edge of the player’s box collider meets the bottom edge of the floating wall, the player gets flung beneath the wall.
Anyone know why this could be happening?
In the attached image, the blue represents what I want it to do, upon reaching the bottom edge of the wall. The red is what it currently does, for some reason or another.
I’ll post my code here for reference.
using UnityEngine;
using System.Collections;
public class PlayerMotor : MonoBehaviour
{
//Variables
private Vector3 moveVector;
private Vector3 lastMove;
public float speed = 8;
public float jumpForce = 12;
public float gravity = 25;
private float verticalVelocity;
private CharacterController controller;
private void Start()
{
//Get Character Controller
controller = GetComponent<CharacterController> ();
}
private void Update()
{
//Set movement vectors
moveVector = Vector3.zero;
moveVector.x = Input.GetAxis ("Horizontal") * speed;
//Apply jump if controller is on the ground
if (controller.isGrounded)
{
verticalVelocity = -1;
if (Input.GetKeyDown (KeyCode.Space))
{
verticalVelocity = jumpForce;
}
}
else
{
//Disable movement while in mid air
verticalVelocity -= gravity * Time.deltaTime;
moveVector = lastMove;
}
moveVector.y = 0;
moveVector.Normalize();
moveVector *= speed;
moveVector.y = verticalVelocity;
//Move player
controller.Move (moveVector * Time.deltaTime);
lastMove = moveVector;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
//Detect if controller has jumped into a wall
if (!controller.isGrounded && hit.normal.y < 0.01f) {
//Apply wall slide
verticalVelocity = -1;
//Apply wall jump
if (Input.GetKeyDown (KeyCode.Space)) {
verticalVelocity = jumpForce;
moveVector = hit.normal * speed;
}
//Exit wall slide
if (Input.GetKeyDown (KeyCode.S)) {
verticalVelocity = 0;
moveVector.x = hit.normal.y;
}
} else
return;
}
}