Example #1
0
    void Start()
    {
        //organ = GameObject.FindWithTag("Organ");
        player = this.GetComponent <PlayerHide>();

        animator = this.transform.GetComponent <Animator>();
    }
Example #2
0
 void Start()
 {
     playerHide     = GameObject.FindGameObjectWithTag("Player").GetComponent <PlayerHide>();
     playerMovement = GameObject.FindGameObjectWithTag("Player").GetComponent <PlayerMovement>();
 }
    void FixedUpdate()
    {
        //Makes sure the enemy is actively following the player.
        if (active)
        {
            //Sets up the distance moved this frame.
            float step = runSpeed * Time.deltaTime;

            //Casts a ray towards the player.
            RaycastHit2D hit = Physics2D.Raycast(transform.position, ((Vector2)target.position - (Vector2)transform.position), sightRadius);
            //Draws a ray towards the position the enemy is moving towards. Debugging purposes.
            Debug.DrawRay(transform.position, (targetVector - (Vector2)transform.position), Color.red);
            //If the cast ray hits the player, the position the enemy is moving towards updates.
            if (hit && hit.collider.gameObject.tag == "Player")
            {
                //Checks if the player is hiding, and if not, sets the targetVector to the player's position.
                PlayerHide playerHide = hit.collider.gameObject.GetComponent <PlayerHide>();
                if (!playerHide.Hiding)
                {
                    nodePath.Pathing = false;
                    step             = runSpeed * Time.deltaTime;
                    targetVector     = target.position;
                }
            }
            else
            //Otherwise, the targetvector is set to self to stop it. Or at least that's what it used to do. Now it turns on
            //the nodepathing component too, which is another script.
            {
                //step = walkSpeed * Time.deltaTime;
                targetVector     = transform.position;
                nodePath.Pathing = true;
            }
            //Checks and records the direction the enemy is moving as signed values, to determine if it's moving up or down/left or right
            horizontalDirection = Mathf.Sign(targetVector.x - transform.position.x);
            verticalDirection   = Mathf.Sign(targetVector.y - transform.position.y);

            //Moves the enemy towards the targeted position.
            if (!avoiding)
            {
                rBD2D.MovePosition(Vector2.MoveTowards(transform.position, targetVector, step));
            }
            else
            {
                rBD2D.MovePosition((Vector2)transform.position + additionalVelocity);
            }

            //Resets whatever velocity was added to this frame
            additionalVelocity = new Vector2(0, 0);
        }

        //The code below calculates the speed the enemy is moving in order to inform Unity's animator.
        xSpeed = transform.position.x - prevX;
        ySpeed = transform.position.y - prevY;

        prevX = transform.position.x;
        prevY = transform.position.y;

        Vector2 magnitude = new Vector2(xSpeed, ySpeed);

        enemyAnimator.SetFloat("HorizontalMovement", xSpeed);
        enemyAnimator.SetFloat("VerticalMovement", ySpeed);
        enemyAnimator.SetFloat("Speed", magnitude.sqrMagnitude);
    }