My Linecast is weird

I have a bot that should chase the player when he sees him. That is the Script for seeing the player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))]
public class Enemy_Sight : MonoBehaviour
{
    [Range(0f, 360f)] public float FOV = 110f;
    [HideInInspector] public Vector3 PersonalLastSighting;
    [HideInInspector] public static Vector3 GlobalLastSighting;
    [HideInInspector] public bool PlayerInSight = false;

    private NavMeshAgent Nav;
    private GameObject Target;
    private float ViewDist = 10;
    private LayerMask ignoreLayer;

    private void Start()
    {
        Nav = GetComponent<NavMeshAgent>();
        Target = GameObject.FindGameObjectWithTag("Player");
        ignoreLayer = LayerMask.GetMask("Enemy_AI");
    }

    private void Update()
    {

        Vector3 direction = Target.transform.position - transform.position;
        float angle = Vector3.Angle(transform.forward, direction);

      
        if (SeesPlayer())
        {
            PersonalLastSighting = Target.transform.position;
            GlobalLastSighting = Target.transform.position;
            PlayerInSight = true;
        }
        else if (Vector3.Distance(Target.transform.position, transform.position) < 2)
        {
            PersonalLastSighting = Target.transform.position;
            PlayerInSight = true;
        }
        else
        {
            PlayerInSight = false;
        }
    }

    bool SeesPlayer()
    {
        if (Vector3.Distance(transform.position, Target.transform.position) < ViewDist)
        {
            Vector3 direction = (Target.transform.position - transform.position).normalized;
            float angle = Vector3.Angle(transform.forward, direction);

            if (angle <= FOV / 2)
            {
                if (!Physics.Linecast(transform.position, Target.transform.position))
                {
                    return true;
                }
            }
        }
        return false;
    }
}

But when I start the Game it collides with the player for like 8 sek and then it works… I dont understand why

Sorry but I am quite new to Unity and I cant fix it

EDIT:
Simple fix just add a line to handle the case of collision with the player too (But if there is another solution please tell me)

Well,you are not showing any code where you are moving the player, or telling the nav agent where to move to. That makes it very difficult to determine where the problem is.

1 Like

The Mistake is there thasts for sure because I know the Error but don know how to solve it in a different way.
The Problem is that the Linecast collides with the player for like 8 sec and that should not happen. After the 8 sec it isn’t colliding anymore. But WHY is that ? Thats the main question.

PS: Sorry for my bad English

Well, we don’t know if what you think is the error really is the error, or what you believe the error to be. How can we help if we don’t know what’s wrong? The code you showed so far seems fine: it looks if the player is within the FOV or within 2m of the enemy, and then sets some variables accordingly. It doesn’t reset PersonalLastSight, but I don’t know if that is a factor because you don’t show any code where that is being used.

1 Like

The Move Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent), typeof(Enemy_Sight))]
public class Enemy_Move : MonoBehaviour
{
    private float AttackRadius = 1.5f;
    private Enemy_Sight sight;
    private NavMeshAgent agent;

    private IEnumerator LookAround_Routin;
    private IEnumerator Attack_Routin;

    private void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        sight = GetComponent<Enemy_Sight>();
    }

    private void Update()
    {
        if (sight.PlayerInSight && Vector3.Distance(sight.PersonalLastSighting, transform.position) > AttackRadius)
        {
            agent.SetDestination(sight.PersonalLastSighting);
        }
        else if (sight.PlayerInSight && Vector3.Distance(sight.PersonalLastSighting, transform.position) <= AttackRadius)
        {
            // Start Attack routin
        }
        else if (!sight.PlayerInSight && transform.position == sight.PersonalLastSighting)
        {
            // Start Lookaround routin (Umschauen)
            // if cant find player stay there
        }
        else if (!sight.PlayerInSight && agent.destination != sight.PersonalLastSighting)
        {
            //agent.SetDestination(sight.PersonalLastSighting);
        }
    }
}

Its not finished and the logic is not really good but i want to override it anyways. That the only other script at the Moment that uses this variables

Oh, wait - your issue isn’t that the enemy collides with the player for the first 8 seconds, but that your LineCast in SeesPlayer() detects a collision. You then assume the collision detcted is the player. How do you know it’s the player and not some other object?

To make sure that LineCast is really being triggered by the correct colliders, replace LineCast with RayCastAll, and Log out all colliders objects that are hit with the cast.

        int layerMask = ~ignoreLayers;
        Ray targetRay = new Ray(gameObject.transform.position, (enemyPosition - playerPosition));
        RaycastHit[] rHits = Physics.RaycastAll(targetRay, 100f, layerMask);

        foreach (RaycastHit hit in rHits)
        {
            Debug.log("Hit collider of " + hit.collider.gameObject.name);
        }

check which objects are hit, and if they really hit the player, not something else. Note that (enemyPosition - playerPosition) is the direction to the player, you may need to turn the vector around (by adding a ‘-’ sign in front).
-ch

Ok Thx