private void ChangeAnimationState(AnimationStates newState)
 {
     if (_currentState == newState)
     {
         return;
     }
     _animator.Play(newState.ToString());
     _currentState = newState;
 }
Exemple #2
0
    private void Update()
    {
        if (targets.Count <= 0)
        {
            foreach (var player in GameObject.FindObjectsOfType <Player>())
            {
                targets.Add(player.transform);
            }
            return;
        }

        Transform closestTarget   = null;
        float     closestDistance = lookRadius;

        foreach (var target in targets)
        {
            float distance = Vector2.Distance(target.position, transform.position);

            if (distance <= lookRadius && distance < closestDistance)
            {
                closestTarget   = target;
                closestDistance = distance;
            }
        }

        if (closestTarget != null)
        {
            _agent.isStopped = false;
            _agent.SetDestination(closestTarget.position);
        }
        else
        {
            if (_agent.isActiveAndEnabled)
            {
                _agent.isStopped = true;
            }
        }

        Vector3 moveDirection = gameObject.transform.position - _origPos;

        if (moveDirection != Vector3.zero)
        {
            float      angle        = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
            Quaternion lookRotation = Quaternion.AngleAxis(angle, Vector3.forward);

            AnimationStates cState = AnimationStates.Default;
            switch (lookRotation.x)
            {
            case -1:
                cState = AnimationStates.WalkLeft;
                break;

            case 1:
                cState = AnimationStates.WalkRight;
                break;
            }

            Debug.Log(cState.ToString());
        }
        _origPos = transform.position;
    }
Exemple #3
0
 public override void execute()
 {
     base.execute();
     character.Play(animationState.ToString());
     StartCoroutine(waitAndTriggerEnd(character.GetCurrentAnimatorClipInfo(0).Length));
 }