private void SwitchWaypoint()
    {
        bool shouldBranch = false;

        //checks if current waypoint has branches and decides whether it should use that branch
        if (currentWaypoint.branches != null && currentWaypoint.branches.Count > 0)
        {
            shouldBranch = UnityEngine.Random.Range(0f, 1f) <= currentWaypoint.branchRation ? true : false;
        }

        if (shouldBranch)
        {
            //picks random branch
            currentWaypoint = currentWaypoint.branches[UnityEngine.Random.Range(0, currentWaypoint.branches.Count - 1)];
        }
        else
        {
            currentWaypoint = currentWaypoint.nextWaypoint;
        }

        if (currentWaypoint == null)
        {
            moving = false;
            ReachedDestination?.Invoke();
        }
        else
        {
            destination = currentWaypoint.GetPosition();
        }
    }
Exemple #2
0
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray        ray = _camera.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit))
            {
                Transform objectHit = hit.transform;

                if (objectHit.GetComponent <InteractableObjectBase>())
                {
                    Debug.Log(objectHit.name);
                    var obj = (InteractableObjectBase)objectHit.GetComponent <InteractableObjectBase>();
                    reachedDestination += obj.Interract;
                }

                _navMeshAgent.destination = new Vector3(hit.point.x, transform.position.y, hit.point.z);
                _moving = true;
            }
        }
        if (_moving)
        {
            if (HasReachedDestination() && reachedDestination != null)
            {
                reachedDestination();
            }
        }
    }