I have a 3d game, but for movement I use only two axes (X and Z) - X is vertical, Z is horizontal
Here's what I'm trying to achieve:
Player is the circle, InputDirection is Blue, NewInputDirection is Green. I'm struggling on getting the desired green direction.
Image 1 The player moves up (he's pushing onto the wall)
InputDirection: (-1f, 0f, 0f)
NewInputDirection: (0f, 0f, 0f) because we should stop him.
Image 2 The player moves towards the wall and the bottom. When he reaches the wall, his horizontal axis should be blocked - set to 0, so he slides)
InputDirection: (0.5f, 0f, -0.5f)
NewInputDirection: (0.5f, 0f, 0f)
Image 3 Same thing as above, but the wall is rotated.
Right now, my code looks like this:
RaycastHit hit;
bool collides = Physics.Raycast(this.playerRigidbody.position, inputDirection, out hit, 1f); // ray in front of the player
if(collides){ // if there's a collision
// calculate the direction from the player to the collision contact point
Vector3 collisionNormal = (hit.point - playerRigidbody.position).normalized;
// idk...
}
I don't know how to calculate it. I've tried various ways. Should I also take the wall's rotation into account?