//Take damage and get knocked back. For when you touched an enemy
    private void TakeTouchDamage(EnemyCharacter enemy)
    {
        if (!damaged && enemy.CanDealDamage())
        {
            //Knockback
            physicsObject.grounded = false;
            physicsObject.SetVelY(0f);
            Vector2 knockbackForce = Vector2.up * PlayerCharacter.Instance.MoveSpeed * Time.fixedDeltaTime / 3.0f;
            knockbackForce.x = -Vector2.right.x * PlayerCharacter.Instance.MoveSpeed * Time.fixedDeltaTime / 3.0f;
            physicsObject.AddForce(knockbackForce);

            PlayerCharacter.Instance.TakeDamage(enemy.TouchDamage);
            damaged = true;
        }
    }
Exemple #2
0
    private void Patrol()
    {
        if (enemy.CanDealDamage())
        {
            if (!patrolling)                            //If you're not patrolling
            {
                patrolling        = true;               //Now you are
                patrolLocStart    = transform.position; //Get the start location
                moveLeft          = !moveLeft;          //Make it move the other way
                curPatrolDistance = Random.Range(1.0f, enemy.MaxPatrolDistance);
            }

            if (Vector2.Distance(patrolLocStart, transform.position) >= curPatrolDistance) //If the enemy has reached the end of their patrol range
            {
                patrolling = false;                                                        //reset the patrolling process
                waiting    = true;                                                         //patrol cooldown
                physicsObject.SetVelX(0f);                                                 //stop them from moving
            }
            else
            {
                if (moveLeft && !physicsObject.LeftBlocked)                   //If the enemy is moving left and the left side is not blocked
                {
                    physicsObject.SetVelX(-enemy.MoveSpeed * Time.deltaTime); //make them move left
                }
                else if (!moveLeft && !physicsObject.RightBlocked)            //If the enemy is moving right and the right side is not blocked
                {
                    physicsObject.SetVelX(enemy.MoveSpeed * Time.deltaTime);  //make them move right
                }
                else
                {
                    patrolling = false;        //if you're blocked, reset the patrolling process
                    waiting    = true;         //patrol cooldown
                    physicsObject.SetVelX(0f); //stop them from moving
                }
            }
        }
        else
        {
            physicsObject.SetVelX(0.0f);
        }
    }