as you can see in the picture, my character go along with wall. it detected wall by capsulecast. (the direction of capsulecasting is forward of character)
when it go straight, capsulecast hit the wall and it's correct. when it go parallel with wall, capsulecast hit nothing and it's correct. but if my character turning at that moment, it hit nothing..
how can i solve this?
(sorry for my english ;-|) and here is my code for detect obstacle.
horRayLength = Mathf.Abs(horMoveAmount.magnitude) + skinWidth;
isHit = Physics.CapsuleCast(capsuleTop, capsuleBottom, capsuleCollider.radius, horMoveAmount.normalized, out raycastHit, horRayLength, horCollisionMask);
if (isHit)
{
if (raycastHit.distance != 0)
{
var projected = Vector3.ProjectOnPlane(horMoveAmount, raycastHit.normal);
moveAmount.x = projected.x;
moveAmount.z = projected.z;
}
}
transform.Translate(moveAmount, Space.World);
CapsuleCast
math looks correct (other than an unnecessaryAbs
— amagnitude
will never give you a negative number). I think the problem may arise in how you computehorMoveAmount
or how you move your capsule. Try showing us a Minimal Complete Verifiable Example: all of the code or scene setup steps we'd need to follow to reproduce the whole problem in a new, empty project. \$\endgroup\$