Weapon Collision problem, Weapon goes through the walls(tps)

Hey!

I’m making a third-person-zombie-shooter game, and I have a little problem.

when I get close to a wall, characters weapon get’s through the wall.

I want to fix it with IK (like COD:Modern Warfare), I want to rotate and move weapon down just like he is holstering the weapon when he is near the wall.

I tried to make it but got weird results…


Thanks :]

I kind of fixed that bug, I shoot a ray from character’s position but I add .1f to position.y to bring it a little bit higher.

then if it hit’s a certain layer’s content, weapon rotates it’s front to the hit point and voila!!!

it has another bug thou, for example when he is in front of a window, he still holster his weapon but I have a solution and that’s shooting another ray from same position but adding more to position.y; if it hits something then we go to second condition but if not, we aim normally!


`if (useWeaponCollision)
{
Vector3 startPosition = playerLocomotion.myTransform.position;
startPosition.y += 1.2f;

Ray ray = new Ray(startPosition, playerLocomotion.myTransform.forward);

if(Physics.Raycast(ray, weaponCollisionCheckDistance, layersToCheckWeaponCollision))
{
    Debug.DrawRay(startPosition, ray.direction, Color.blue, .1f);

    startPosition.y -= 1f;
    ray = new Ray(startPosition, playerLocomotion.myTransform.forward);

    if (Physics.Raycast(ray, out RaycastHit hit, weaponCollisionCheckDistance, layersToCheckWeaponCollision))
    {
        targetDirection = hit.point - aimPivot.position;

        if (targetDirection == Vector3.zero)
            targetDirection = aimPivot.forward;

        targetRotation = Quaternion.LookRotation(targetDirection);

        Debug.DrawLine(startPosition, hit.point, Color.blue, .1f);
        Debug.DrawRay(startPosition, ray.direction, Color.blue, .1f);
    }
}

}`

and this works just fine!
I would love to see other solutions :slight_smile: