Exemplo n.º 1
0
 // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
 override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
 {
     rb       = animator.GetComponent <Rigidbody2D>();
     pathfind = animator.GetComponent <EnemyPathfinding>();
     AI       = animator.GetComponent <EnemyAI>();
     player   = pathfind.GetTargetTransform();
 }
Exemplo n.º 2
0
    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateinfo, int layerindex)
    {
        float reachedPositionDistance = 1f;

        if (AI.GetState() == EnemyAIState.ChaseTarget)
        {
            if (Vector3.Distance(animator.transform.position, new Vector2(pathfind.GetTargetTransform().x, rb.position.y + 1)) < 0.5f && Vector3.Distance(animator.transform.position, new Vector2(pathfind.GetTargetTransform().x, rb.position.y - 1)) > -0.5f)
            {
                //Debug.Log("Deadzone alue idiootti");
            }
            else
            {
                if (pathfind.LookAtPlayer())
                {
                    Vector2 target = new Vector2(pathfind.GetTargetTransform().x, rb.position.y); //pathfind.GetTargetTransform().position; //new Vector2(player.position.x, rb.position.y);
                    Vector2 newPos = Vector2.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
                    CheckFacingDirection(animator, newPos);
                    if (Vector3.Distance(animator.transform.position, target) < reachedPositionDistance)
                    {
                        AI.SetState(EnemyAIState.Attacking);
                        return;
                    }
                    else
                    {
                        rb.MovePosition(newPos);
                    }
                }
            }
        }

        else
        {
            Vector2 target = new Vector2(pathfind.GetTargetTransform().x, rb.position.y); //pathfind.GetTargetTransform().position; //new Vector2(player.position.x, rb.position.y);
            Vector2 newPos = Vector2.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
            CheckFacingDirection(animator, newPos);
            rb.MovePosition(newPos);
        }
    }