0
\$\begingroup\$

enter image description here

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);
\$\endgroup\$
4
  • \$\begingroup\$ Your CapsuleCast math looks correct (other than an unnecessary Abs — a magnitude will never give you a negative number). I think the problem may arise in how you compute horMoveAmount 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\$
    – DMGregory
    Commented Feb 28, 2022 at 12:09
  • \$\begingroup\$ @DMGregory I added code to get horMoveAmount. my character move by arrow key and rotate to move direction. \$\endgroup\$
    – Hree
    Commented Feb 28, 2022 at 13:42
  • \$\begingroup\$ @DMGregory I think this problem caused from getting projected vector. i want my character move along with wall so i use projectOnPlane, but it maybe return longer value so my character go through the wall. what should i do to get exactly same moveAmount when projected? \$\endgroup\$
    – Hree
    Commented Feb 28, 2022 at 14:21
  • \$\begingroup\$ Here's how I've managed a similar task in the past. \$\endgroup\$
    – DMGregory
    Commented Feb 28, 2022 at 14:27

0

You must log in to answer this question.

Browse other questions tagged .