Raycast hit.point Problem

Hello guys, i follow the Little Adventurer: Learn to make a 3D action game with Unity tutorial of Single-Minded Ryan in udemy, to make a 3d action game and i have an issue about raycast hit for damage system.
I added a box collider to another object on child of my character and set the box collider, i just wanna create raycasthit but raycast hit is stuck some point and doesn’t work like i wanted.

Here is my hit:

It doesn’t go through z-axis of box collider, why it happened? Here’s my gizmos method:

private void OnDrawGizmos()
{
    damageCasterCollider = GetComponent<Collider>();
    if(damageCasterCollider == null)
    {
        Debug.Log("a");
            
    }
    RaycastHit hit;

    Vector3 originalPos = transform.position + (-damageCasterCollider.bounds.extents.z) * transform.forward;

    bool isHit = Physics.BoxCast(originalPos, damageCasterCollider.bounds.extents / 2, transform.forward , out hit, transform.rotation, damageCasterCollider.bounds.extents.z, 6);;
    Debug.DrawLine(originalPos, hit.point);
    Gizmos.color = Color.yellow;
    Gizmos.DrawSphere(hit.point, 0.3f);

    if (isHit)
    {
        
        Gizmos.color = Color.yellow;
        Gizmos.DrawSphere(hit.point, 0.3f);
    }
}

1 Answer

1

From what I understand you want to detect enemies in front of the player and then use a BoxCast to detect gameObjects with the "Enemy" tag only.

Gizmos only appear within the editor and not when playing, so why is it that you are calling this method using OnDrawGizmos() instead of when the player performs an input?