public void StateExecute()
    {
        Vector3 toTarget = target.position - transform.position;
        Vector3 toTargetIgnoringHeightDir = new Vector3(toTarget.x, 0, toTarget.z).normalized;

        if (toTarget.magnitude < attackRange)
        {
            if (IsInSightView(toTargetIgnoringHeightDir))
            {
                myStateMachine.SetState <MyMeleeAttackState>();
                return;
            }
        }

        Vector3    toTargetFromRayPoint = target.position - raycastInitialPoint.position;
        RaycastHit hit;

        if (Physics.Raycast(raycastInitialPoint.position, new Vector3(toTargetFromRayPoint.x, 0, toTargetFromRayPoint.z).normalized, out hit, Mathf.Infinity, layerRay))
        {
            Player player = hit.collider.GetComponent <Player>();
            if (player == null)
            {
                myStateMachine.SetState <GoToPathPointState>();
                return;
            }
        }

        Vector3 avoidanceDir = GetObstacleAvoidanceDir();
        Vector3 avoidanceDirHeightIgnored = new Vector3(avoidanceDir.x, 0, avoidanceDir.z).normalized;
        Vector3 finalAvoidance            = avoidanceDirHeightIgnored * avoidanceAmount;

        Debug.DrawLine(transform.position, transform.position + finalAvoidance, Color.yellow);
        transform.forward = Vector3.Lerp(transform.forward, (toTargetIgnoringHeightDir + finalAvoidance).normalized, rotationSpeed * Time.deltaTime);
        moveBehaviour.SetVelocity(transform.forward);
        animator.SetFloat("Speed", 1);
    }
Exemple #2
0
    private void FollowPath()
    {
        float   range       = 2f;
        Vector3 toPathIndex = new Vector3();

        if (path != null)
        {
            toPathIndex = path[index].position - transform.position;
        }
        float distanceToNextWaypoint = toPathIndex.magnitude;

        if (distanceToNextWaypoint < range)
        {
            index++;
            return;
        }
        Vector3 dirInterpolation = Vector3.Lerp(transform.forward, new Vector3(toPathIndex.x, 0, toPathIndex.z).normalized, rotationSpeed * Time.deltaTime);

        transform.forward = new Vector3(dirInterpolation.x, 0, dirInterpolation.z).normalized;
        moveBehaviour.SetVelocity(transform.forward);
        animator.SetFloat("Speed", 1);
    }