Wall sliding with characterController against an edge
I'm trying to figure out how to make characterController slide along an edge as it would slide along a wall. I'm casting a ray starting forward (a little scaled back so the character can get closer to the edge) and shooting straight down:
forwardRay = transform.position + transform.forward * 0.4f;
RaycastHit hitInfo = default(RaycastHit);
var raycast = Physics.Raycast(forwardRay, Vector3.down, out hitInfo, Mathf.Infinity, 1);
Then, I find the floor point directly below the capsule:
RaycastHit hitInfo1 = default(RaycastHit);
var raycastl = Physics.Raycast(transform.position, Vector3.down, out hitInfo1, Mathf.Infinity, 1);
Finally, I fire a ray back from the point forward where there is no ground, but at the height of the point where there is ground beneath the player:
ground = hitInfo1.point;
RaycastHit hitInfo1 = default(RaycastHit);
var raycast2 = Physics.Raycast(new Vector3(forwardRay.x, ground.y + -0.02f, forwardRay.z), new Vector3((-transform.forward).x, 0f, (-transform.forward).z), out hitInfo1, Mathf.Infinity, 1);
This gives me the a ray at the edge of where my capsule is trying to move: https://gfycat.com/costlyblushingdragonfly
I then take the point of where this ray hits, subtracted from my forwardRay and multiplied by my speed to give me an edge vector:
edgeVector = (forwardRay - hitInfo2.point) * Mathf.Abs(currentSpeed) * 10f;
that I can subtract from my final movement:
GetComponent<CharacterController>().Move((Quaternion.Euler(transform.eulerangles) * new Vector3(0f, gravity, currentSpeed) - edgeVector) * Time.deltatime);
Here's the full code I'm using to get the edge:
void Update()
{
forwardRay = transform.position + transform.forward * 0.4f;
RaycastHit hitInfo = default(RaycastHit);
var raycast = Physics.Raycast(forwardRay, Vector3.down, out hitInfo, Mathf.Infinity, 1);
if (raycast)
{
edgeVector = Vector3.zero;
}
else
{
RaycastHit hitInfo1 = default(RaycastHit);
var raycastl = Physics.Raycast(transform.position, Vector3.down, out hitInfo1, Mathf.Infinity, 1);
if (raycast1)
{
ground = hitInfo1.point;
RaycastHit hitInfo1 = default(RaycastHit);
var raycast2 = Physics.Raycast(new Vector3(forwardRay.x, ground.y + -0.02f, forwardRay.z), new Vector3((-forwardRay).x, 0f, (-forwardRay).z), out hitInfo1, Mathf.Infinity, 1);
if (raycast2)
{
edgeVector = (forwardRay - hitInfo2.point) * Mathf.Abs(currentSpeed) * 10f;
}
}
}
}
}
The issue I'm having now is that it works when everything starts at 0,0,0: https://gfycat.com/unselfishdevotedbird
But totally breaks when the capsule and floor is moved. This is with the floor and capsule moved along the x and z axis by 2: https://gfycat.com/envioustornafricanelephant
And by the time it gets to 9 on the x and z axis, the capsule just falls off: https://gfycat.com/impeccableinferiorfantail
I know this is because it's casting in the wrong direction, but when I correct it by casting directly backwards:
var raycast2 = Physics.Raycast(new Vector3(forwardRay.x, ground.y + -0.02f, forwardRay.z), new Vector3((-transform.forward).x, Of, (-transform.forward).z), out hitInfol, Mathf.Infinity, 1);
the wall sliding effect breaks: https://gfycat.com/greentestyirukandjijellyfish
So, obviously all along the wall sliding effect was an accident, but I need to figure out either adjust my charactercontroller movement to slide against the correct vector, or find a relative vector direction I can use that would give the same effect.
Your answer
Follow this Question
Related Questions
Find a point in space using Vector3 angles and Raycast 1 Answer
Raycast under crosshair 0 Answers
Acquiring a negative angle? 1 Answer
SignedAngle returning wrong angles 0 Answers
How to properly make an object follow the edge of another object? 0 Answers