So my original design kinda works, but has some issues. It goes like this:
- Use Vector2.MoveTowards to get a steeringVector based on my current velocity and input movement
- Cast along the steeringVector, looking for obstacles
- None? MovePosition
- Otherwise do some geometry to find a deflection vector to slide along the obstacle
- If the deflection vector is v. smol, just stop (we are too close to a right angle collision to bother moving)
Otherwise:
- Cast along the deflection vector
- No obstacles? MovePosition (aka, slide along the wall)
- Otherwise, don’t move (you’re in a corner of some sort)
So this generally works, with some quirks. Namely, the faster you approach an obstacle, you further away from it you stop (or begin sliding tangentially along it)
So I was considering, instead:
- Look at the first cast, and move to the hit.centroid
- Calculate the fraction of the steeringVector we’ve traveled.
- Calculate the deflection vector and use whatever fraction we have yet to move to slide along the wall.
This seems like it almost would work, but there are some issues:
- If I move to the hit.centroid, I get stuck in the wall. I instead move to the centroid minus an offset (this kinda makes sense with float math, I’m not suprised I need a buffer)
- MovePosition doesn’t happen immediately, so the deflection cast doesn’t actually happen from alongside the wall, it happens from the same position as the first cast.
This second issue is the kicker… It can cause wall clipping/getting stuck on weird geometry, especially when moving fast.
Questions:
- Is there a handy way to perform the second cast from the “wall hugging” position without waiting a frame for MovePosition to actually move the rigidbody there (which seems untenable)?
- Am I overcomplicating this? Are there better approaches to obstacle sliding a kinematic rigidbody?