Using ProjectOnPlane to slide around adjacent edges
I'm using three raycasts to find the edge directly in front of my characterController capsule and store the normal:
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 raycast1 = 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((-transform.forward).x, Of, (-transform.forward).z), out hitInfol, Mathf.Infinity, 1);
if (raycast2)
{
edgeVector = hitInfo2.normal;
}
}
}
}
}
I then use ProjectOnPlane to ensure the characterController slides along the edge instead of just stopping dead in it's tracks:
GetComponent<CharacterController>().Move(Vector3.ProjectOnPlane(Quaternion.Euler(transform.eulerangles) * new Vector3(0f, gravity, currentSpeed), edgeVector) * Time.deltatime);
The trouble is, it doesn't handle adjacent corners at all and just falls off, since the raycast is only finding the edge normal directly in front of the capsule. Here you can see the red ray is my forward vector in worldspace, and it fires back from there (pink ray) towards the height of the ground beneath my capsule (blue ray): https://gfycat.com/delectableslipperygosling
Is it possible to find the next normal in the direction I'm moving, or better yet, wrap the ProjectOnPLane vector around the nearest normal in it's projected direction? Otherwise, I'm just doing this the wrong way?
Answer by streeetwalker · Apr 09, 2020 at 07:04 PM
@Zebbi, are you rotating the player to face the direction of motion? Because it in your gif doesn't look like the ray is in front of the direction of motion toward the end of the video. If it's not rotating then you need 4 ray casts - one for each face.
But either way, I'm not sure what you want to do when you get to a corner: If you want to keep moving around the corner, then I think you need to detect if any two rays are on two different edges/faces at the same time - indicating the player is in a corner, and then you can rotate the player to face the new direction of motion, or what ever you want to do from there.
The player is facing the direction of motion and the ray rotates with the player.
The red ray is essentially forward + position shooting down
The blue ray is just the capsule's position shooting down
The pink ray originates from the x/z of the red ray and the y of the blue ray, shooting -forward of the capsule.
When the capsule hits the edge and forward motion is still being added, I just want it to stop there. If the capsule is rotated back for forth, it should continue to slide along either edge. Here you can see (https://gfycat.com/violetadolescentfrigatebird) that I'm deliberately sliding around the corner and the capsule slides exactly as intended, but I just need to stop it from going off the edge when it hits the corner from either direction. It just needs to stop and stay still until the capsule is manually rotated away from the corner.
OK, it seems you need more than one edge detection ray. Your red ray is generated as intended to detect the edge as you slide along an edge. While you are sliding along an edge you need to be casting a ray at a 90 degree angle to that in the direction of motion to deter$$anonymous$$e if you hit another edge - your pink ray doesn't appear to do that, and it isn't really visible in the gif.
If you suddenly find yourself detecting two edges at the same time, you know you are in a corner, and then stop any motion. If you rotate at that point, rather than rotating the edge detection with the object, you need maintain them in a planes perpendicular to the edges you've detected, so you don't lose the edge detection that tells you that the object is in the corner.
The thing is, your sliding along the edge and it appears, as you've indicate, your moving directly into the edge. So the object is not facing the resultant direction of motion, so it kind of confuses the problem. Then when you get into a corner and rotate, it seems your rotating the edge detection and the object loses it, so it no longer knows it's in a corner any more...
Okay, so I need edge detection on both sides as well as forwards, the problem would be if the character was on a tight strip they wouldn't slide correctly as both sides would detect an edge, and technically that would count as a corner. ProjectOnPlane seems to be giving me the edge sliding effect perfectly, but really it the actual vector needs to be culled to the length of the normal in the direction we're travelling towards, so it can just stop when we get there (0,0,0 the vector until we rotate in a direction that it can move). Is there any other way of calculating the ortho vector perpendicular to the normal we're sliding along that wouldn't just keep going along the normal? Or is there a way of doing the edge detection that would work in all circumstances?
Your answer
Follow this Question
Related Questions
Acquiring a negative angle? 1 Answer
Dot Product Help 0 Answers
Vector3.Lerp not moving object backwards 0 Answers
How to get the angle to animate the player when running? 0 Answers