0
\$\begingroup\$

I'm working in Unity3d 2019.2.0f1 and I'm trying to make a chasing bot that follows a certain GameObject with the "Player" tag.

I'm not yet attempting a field of view approach.

How the Script works:

Step 1 : Overlap a sphere on the Bot AI's transform.position and put all the things in the sphere inside a Collider[].

Step 2: Go through each collider in ColliderArray and look for a GameObject with the tag "Player".

Step 3: If found shoot a Raycast to said GameObject with tag.

Step 4: If the Gameobject hit does not have the "Player" tag, then the "Player" is being blocked by a obstacle therefor not in the line of sight, if the GameObject isn't blocked by an obstacle then we set the destination of the referenced Navmeshagent.

The problem I'm finding (with about 3000 Debug.Logs) is that the Raycast only shoots around 7 times and then just stops, also the "Player" needs to stand in a very specific random location (even tho the "Player" is in plane sight).

What am I doing wrong here?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
    NavMeshAgent agent;

    public float LookRadius = 20; //unity units

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

    // Update is called once per frame
    void Update()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, LookRadius);

        foreach(Collider Object in colliders)
        {
            if(Object.transform.CompareTag("Player"))
            {
                RaycastHit hit;

                if(Physics.Raycast(transform.position, Object.transform.position, out hit, Vector3.Distance(transform.position, Object.transform.position)))
                {
                    //if not true the we hit a obstacle.
                    if(hit.transform.CompareTag("Player"))
                    {
                        agent.SetDestination(hit.transform.position);
                    }
                }
            }
        }
    }
}
\$\endgroup\$
12
  • \$\begingroup\$ so, in your line of code "if(physics.Raycast(transform.position," etc.what exactly are you doing? (I don't understand raycast code very well, but I've never seen it written like that.) \$\endgroup\$ – Millard Oct 6 '19 at 15:30
  • \$\begingroup\$ When you do a raycast in an if statement it carries on when you hit something. it does exactly the same otherwise \$\endgroup\$ – Dan-mechanics Oct 6 '19 at 15:43
  • \$\begingroup\$ ok, is the vector 3 calculating the distance between the raycast point and the player? \$\endgroup\$ – Millard Oct 6 '19 at 15:59
  • \$\begingroup\$ No the vector3.Distance calculates the distance between the Bot and the player, therefor the raycast will be the perfect length \$\endgroup\$ – Dan-mechanics Oct 6 '19 at 16:01
  • \$\begingroup\$ one thing that confuses me, is that you have a comment that says //if not true that we hit a obstacle. but the line under that doesn't seem to check whether you hit an obstacle, it seems to check whether the object you hit is the player. \$\endgroup\$ – Millard Oct 6 '19 at 16:12
1
\$\begingroup\$

According to documentation the second parameter of Physics.Raycast (in the shape you're using) is a direction, not a position. So you have to compute the direction where you want to cast your ray from the agent.

You can get the direction by (target.position - transform.position).normalized.

Also, for optimization sake (not related to your question), it appears that the OverlapSphere call that you're doing is not necessary. You have probably a way to get the Players transforms in your code (via a singleton pattern or something else). Doing so you would avoid looping on every single object that contains a collider and that is around your agent. A simple call to Vector3.Distance() would be necessary to know if the player is close enough of your agent. Looping on every colliders of your scene for every agent would be overkill imho.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks! your solution worked perfectly. i will also take into account the performance tips. \$\endgroup\$ – Dan-mechanics Oct 9 '19 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.