private void Chase()
    {
        attackDetector.gameObject.SetActive(true);
        // move in direction of player
        if (playerReference.transform.position.x >= transform.position.x)
        {
            entityMovement.MoveInDirection(1);
        }
        else
        {
            entityMovement.MoveInDirection(-1);
        }

        GameObject objectHit = raycastHelper.CheckForObjectHits();

        if (objectHit != null)
        {
            if (objectHit.tag == "Ground")
            {
                entityMovement.Jump();
            }
        }
        else if (groundedController.GetIsGrounded() &&
                 raycastHelper.CheckForLedges() &&
                 playerReference.transform.position.y > transform.root.position.y)
        {
            entityMovement.Jump();
        }
    }
Ejemplo n.º 2
0
    public void MoveInDirection(float direction)
    {
        currentMoveSpeed = entityRigidbody.velocity.x;
        float deaccelerationDirection = (-1) * (currentMoveSpeed / Mathf.Abs(currentMoveSpeed));

        if (!entityAttack.GetIsAttacking())
        {
            // Sets the direction the entity is facing
            if (direction > 0)
            {
                transform.root.transform.eulerAngles = new Vector3(0, 0, 0);
            }
            else if (direction < 0)
            {
                transform.root.transform.eulerAngles = new Vector3(0, 180, 0);
            }

            // Applies the correct acceleration, depending on the situation
            if (direction != 0 && Mathf.Abs(currentMoveSpeed) <= maxMovespeed)
            {
                if (direction == deaccelerationDirection)
                {
                    Deaccelerate(deaccelerationDirection);
                }
                else
                {
                    Accelerate(direction);
                }
            }
            // The entity stopped inputting movement but hasn't stopped moving yet
            else if (direction == 0 && Mathf.Abs(currentMoveSpeed) > 0)
            {
                Deaccelerate(deaccelerationDirection);
            }

            float modifiedGravity = Physics2D.gravity.y;
            if (entityRigidbody.velocity.y < 0)
            {
                modifiedGravity *= (2.5f * Time.deltaTime);
            }
            else
            {
                modifiedGravity *= (1.5f * Time.deltaTime);
            }

            Vector2 moveAmount = new Vector2(currentMoveSpeed, entityRigidbody.velocity.y + modifiedGravity);
            entityRigidbody.velocity = moveAmount;
        }
        else
        {
            StopAllMovement();
        }
        if (animator != null)
        {
            animator.SetBool("isRunning", entityRigidbody.velocity.x != 0 || direction != 0);
            animator.SetBool("isGrounded", groundedController.GetIsGrounded());
        }
    }