void FixedUpdate()
 {
     // Move the character
     controller.Move(horizontalMovement * Time.fixedDeltaTime, false, jump);
     jump = false;
 }
Exemple #2
0
    public override void Update()
    {
        base.Update();

        //INVINCIBILITY FRAMES
        if (tempInvulnTimer > 0)
        {
            tempInvulnTimer -= Time.deltaTime;
            if (tempInvulnTimer <= 0)
            {
                tempInvuln      = false;
                tempInvulnTimer = 0;
            }
        }

        float targetVelX;
        float targetVelY;

        _anim.SetBool("InputA", inputA);
        _anim.SetBool("InputB", inputB);
        _anim.SetBool("InputC", inputC);
        _anim.SetBool("InputD", inputD);
        _anim.SetBool("RockThrowWindUp", inputDHold);
        _anim.SetBool("FaceLeft", facingLeft);
        _anim.SetBool("FaceRight", facingRight);
        _anim.SetBool("IsMoving", isMoving);
        _anim.SetInteger("Health", playerHealth);

        switch (state)
        {
        case PlayerStates.immobile:
            // Player cannot control character. Used for cutscenes. Does nothing here, and state gets set manually.
            break;

        case PlayerStates.mobile:
            // Primary state. Player is normal. Can move and attack based on inputs. Check for input code here.
            PlayerMovement();
            //PlayerMovement360();
            if (canAttack != false)
            {
                PlayerAction();
            }
            break;

        case PlayerStates.attacking:
            PlayerAction();
            break;

        case PlayerStates.clownDrill:
            DrillMovement();
            //DrillMovement360();
            break;

        case PlayerStates.dodging:
            // Player is currently dodging. Cannot move. Must wait until dodge is complete, then set state back to moving.
            // TODO: Move player with dodge variable that shrinks (5...4...3...). Once it's 0, setState(PlayerStates.moving).
            targetVelX = dodgeDirection.x * dodgeSpeed;
            targetVelY = dodgeDirection.y * dodgeSpeed / 2;
            _vel.x     = Mathf.SmoothDamp(_vel.x, targetVelX, ref velocityXSmoothing, .1f);
            _vel.y     = Mathf.SmoothDamp(_vel.y, targetVelY, ref velocityYSmoothing, .1f);
            _controller.Move(_vel * Time.deltaTime);

            dodgeSpeed = Mathf.Max(0, dodgeSpeed - friction);
            break;

        case PlayerStates.shoulderdash:
            // Player is currently charging. Cannot move. Must wait until dodge is complete, then set state back to moving.
            targetVelX = dodgeDirection.x * dodgeSpeed;
            targetVelY = dodgeDirection.y * dodgeSpeed / 2;
            _vel.x     = Mathf.SmoothDamp(_vel.x, targetVelX, ref velocityXSmoothing, .1f);
            _vel.y     = Mathf.SmoothDamp(_vel.y, targetVelY, ref velocityYSmoothing, .1f);
            _controller.Move(_vel * Time.deltaTime);

            dodgeSpeed = Mathf.Max(0, dodgeSpeed - friction);
            break;

        case PlayerStates.stunned:
            // Player is stunned. Can't do anything. Must wait until stun is over, then set state to moving.
            // TODO: Make a stun timer. When stunned, set to the stun time. In here, subtract deltatime from th stun timer. When stunTimer <= 0, setState(PLayerStates.moving)
            targetVelX = dodgeDirection.x * dodgeSpeed;
            targetVelY = dodgeDirection.y * dodgeSpeed / 2;
            _vel.x     = Mathf.SmoothDamp(_vel.x, targetVelX, ref velocityXSmoothing, .1f);
            _vel.y     = Mathf.SmoothDamp(_vel.y, targetVelY, ref velocityYSmoothing, .1f);
            _controller.Move(_vel * Time.deltaTime);

            dodgeSpeed = Mathf.Max(0, dodgeSpeed - friction);
            break;

        case PlayerStates.dying:
            // Player is dead. Don't do anything. Game will end now.
            break;

        default:
            // Player is stateless. Do a dance?
            break;
        }
    }
    protected virtual void Update()
    {
        base.Update();

        // Move enemy based on force.
        _controller.Move(new Vector3(xForce, yForce, 0));
        if (xForce > friction)
        {
            xForce -= friction;
        }
        else if (xForce < -friction)
        {
            xForce += friction;
        }
        else if (xForce != 0)
        {
            xForce = 0;
        }
        if (yForce > friction)
        {
            yForce -= friction;
        }
        else if (yForce < -friction)
        {
            yForce += friction;
        }
        else if (yForce != 0)
        {
            yForce = 0;
        }

        switch (state)
        {
        case EnemyStates.spawn:
            break;

        case EnemyStates.move:
            MoveToPlayer();
            CheckToAttack();
            break;

        case EnemyStates.stand:
            CheckToAttack();
            break;

        case EnemyStates.attack:
            // Enemy animates, but otherwise doesn't do anything. The animation state will call for change.
            break;

        case EnemyStates.shoot:
            // Enemy animates, but otherwise doesn't do anything. The animation state will call for change.
            break;

        case EnemyStates.paceBack:
        case EnemyStates.paceForth:
            MoveToTarget(_paceTarget);
            CheckToAttack();
            break;

        case EnemyStates.stun:
            break;

        case EnemyStates.dead:
            break;
        }

        // Tick down the cooldowns.
        if (_attackCooldown > 0)
        {
            _attackCooldown -= Time.deltaTime;
            if (_attackCooldown < 0)
            {
                _attackCooldown = 0;
            }
        }
        if (_gunCooldown > 0)
        {
            _gunCooldown -= Time.deltaTime;
            if (_gunCooldown < 0)
            {
                _gunCooldown = 0;
            }
        }

        if (_paceTimer > 0)
        {
            _paceTimer -= Time.deltaTime;
            if (_paceTimer < 0)
            {
                if (state == EnemyStates.paceBack)
                {
                    setState(EnemyStates.paceForth);
                }
                else if (state == EnemyStates.paceForth)
                {
                    setState(EnemyStates.paceBack);
                }
            }
        }

        if (_anim != null)
        {
            _anim.SetFloat("Health", _enemHealth);
            _anim.SetBool("FacingLeft", facingLeft);
            _anim.SetInteger("PlayerHealth", _playerControl.playerHealth);
        }

        // Confine the enemy to the level boundaries.
        if (state != EnemyStates.dead)
        {
            this.transform.position = LevelBoundary.adjustPositionToBoundary(this.transform.position);
        }

        if (_useRage && _rageLevel > 0)
        {
            // Rage meter ticks down fast. Otherwise it's a second per damage point; way too long.
            _rageLevel -= Time.deltaTime * rageDecayPerSec;
            rageMeter.setDirection(facingLeft);
            rageMeter.subtractRageLevel(Time.deltaTime * rageDecayPerSec);
            if (_enraged && _rageLevel <= 0)
            {
                _enraged = false;
            }
        }
    }