0
\$\begingroup\$

I have an issue with my character controller. I'm using a raycast from the enemy AI as a line of sight in order to start following the player. My issue is that with my current character controller collider offset the raycast won't work. When I zero out the character controlle'rs center all is well but, I don't want that since my character is hovering of sorts so if I zero it out it stands bellow it. Can I get some recommendations of what to do in order to fix that?

This is my CheckLineOfSight function:

    private bool CheckLineOfSight(DummyChar player)
    {
        Vector3 Direction = (player.transform.position - transform.position).normalized;
        float DotProduct = Vector3.Dot(transform.forward, Direction);

        if (DotProduct >= Mathf.Cos(FieldOfView))
        {
            Debug.Log("Can start checking for player Ray " + Direction);
            RaycastHit Hit;
            if (Physics.Raycast(transform.position, Direction, out Hit, Collider.radius, LineOfSightLayers))
            {
                Debug.Log("hited " + Hit.collider.name);
                if (player!= null)
                {
                    Debug.Log("Found target");
                    OnGainSight?.Invoke(player);
                    return true;
                }
            }
        }
        return false;
    }

As I mentioned, by using a dummy character with the character controller in default values, the enemy does see the player as it reaches its line of sight and does go towards him, but with my actual player that has its character controller center offset it does not see it.

Edit : here is the inspector view of the controller and player transform

character view

Although i just realized i might be able to truly zero things out by placing the mesh into a child instead of the root

\$\endgroup\$
1
  • \$\begingroup\$ Can you show us how your character controller is set up in the scene and inspector, so we can see where its transform origin sits relative to the mesh/capsule? It looks like you need to add an offset to the player transform position when computing the Direction variable, but we'd need to see it to estimate what the right offset should be. \$\endgroup\$
    – DMGregory
    Nov 22, 2022 at 21:49

1 Answer 1

0
\$\begingroup\$

So it seems i fixed things by adding a center point in my character.

Vector3 Direction = (player.hitCenter.position - transform.position).normalized;

float DotProduct = Vector3.Dot(transform.forward, Direction);

By adding a reference point for the ray to fall it s now registering it properly, it seems that the ray was shooting down at the feet and since there is nothing there it could not find the target

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .