If I wedge the player object into the corner, it will sometimes start to climb the objects. Anyone know why?
Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float MovementSpeed = 3f;
public LayerMask collisionLayerMask;
private bool IsCollidingNextFrame(Vector3 direction, float predictionTime)
{
// Predict the player’s position in the next frame based on the speed and direction
Vector3 predictedPosition = transform.position + direction * MovementSpeed * predictionTime;
// Get the player’s collider for its size
Collider playerCollider = GetComponent();
Vector3 playerSize = playerCollider.bounds.size;
// Calculate the player’s half size for the BoxCast
Vector3 playerHalfSize = 0.5f * playerSize;
// Check for collisions using BoxCast
RaycastHit hitInfo;
bool isColliding = Physics.BoxCast(transform.position, playerHalfSize, direction, out hitInfo, transform.rotation, MovementSpeed * predictionTime, collisionLayerMask);
return isColliding;
}
void Start()
{
}
void Update()
{
float horizontalInput = Input.GetAxis(“Horizontal”);
float verticalInput = Input.GetAxis(“Vertical”);
Vector3 direction = new Vector3(horizontalInput, 0f, verticalInput);
bool willCollideInNextFrame = IsCollidingNextFrame(direction, Time.deltaTime);
if (!willCollideInNextFrame)
{
transform.Translate(direction * MovementSpeed * Time.deltaTime);
}
else
{
Debug.Log(“Collision detected in the next frame.”);
}
}
}