private void Update() { // Collision info CharacterPhysics2D.CollisionInfo col = Physics.Collisions; // 0 vertical velocity when character is squeezed between a floor and ceiling if ((col.above) || (col.below)) { velocity.y = 0; } // Jumping if (jumpActivated && col.below) { velocity.y = jumpVelocity; // Jumping sound if (jumpSounds.Count > 0) { audio.PlayOneShot(jumpSounds[Random.Range(0, jumpSounds.Count)]); } } Vector2 input = DirectionalInput; // Walking sound if (input.x != 0 && col.below) { if (!walkingAudio.isPlaying) { walkingAudio.Play(); } } else { walkingAudio.Stop(); } // Horizontal velocity smoothing (if no player input is detected) targetVelX = input.x * moveSpeed; if (input.x == 0) { velocity.x = Mathf.SmoothDamp(velocity.x, targetVelX, ref velXSmoothing, xVelocitySmoothTime); } else { velocity.x = targetVelX; } // Gravity velocity += gravityScale * Physics2D.gravity * Time.deltaTime; // Move the character Physics.Velocity = velocity; // Clear flags jumpActivated = false; }
void DecisionFSM() { distance = Vector3.Distance(transform.position, target.transform.position); float xDist = Mathf.Abs(target.position.x - transform.position.x); switch (currentAIState) { case AIState.Patrolling: // Movement velocity.x = movingRight ? patrolSpeed : -patrolSpeed; anim.SetBool("Walking", true); // Flip movement direction at walls CharacterPhysics2D.CollisionInfo col = controller.Collisions; if (col.below) { velocity.y = 0; } if ((col.right || transform.position.x > patrolMaxX) && movingRight) { movingRight = false; } else if ((col.left || transform.position.x < patrolMinX) && !movingRight) { movingRight = true; } FaceMovementDir(); // Next state if (distance < wakeRange) { currentAIState = AIState.Chasing; } break; case AIState.Chasing: // Chase player as long as player is in range if (ExclusiveBetween(xDist, 0.1f, wakeRange)) { velocity.x = (transform.position.x < target.position.x) ? chaseSpeed : -chaseSpeed; anim.SetBool("Walking", true); } else { velocity.x = 0; anim.SetBool("Walking", false); } FaceMovementDir(); // Next state if (xDist >= wakeRange) { currentAIState = AIState.Patrolling; } else if (xDist < startAttackingDistance) { currentAIState = AIState.Attacking; } break; case AIState.Attacking: velocity.x = 0; anim.SetBool("Walking", false); // Attack the player AttackFSM(); FaceTarget(); // Next state if (distance >= stopChaseDistance) { currentAIState = AIState.Chasing; } break; default: currentAIState = AIState.Patrolling; break; } }
void Update() { // Player input float xInput = Input.GetAxisRaw("Horizontal"); float yInput = Input.GetAxisRaw("Vertical"); bool jumpPressed = Input.GetKeyDown(KeyCode.Space); Vector2 input = new Vector2(xInput, yInput); // Collision info CharacterPhysics2D.CollisionInfo col = physics.Collisions; // 0 vertical velocity when character is squeezed between a floor and ceiling if ((col.above) || (col.below)) { velocity.y = 0; } // Jumping if (jumpPressed && col.below) { velocity.y = JumpVelocity; anim.SetTrigger("Jump"); } // Horizontal velocity smoothing (if no player input is detected) targetVelX = input.x * MoveSpeed; if (input.x == 0) { velocity.x = Mathf.SmoothDamp(velocity.x, targetVelX, ref velXSmoothing, XVelocitySmoothTime); } else { velocity.x = targetVelX; } // Gravity velocity += gravityScale * Physics2D.gravity * Time.deltaTime; // Move the character physics.Velocity = velocity; // Attack logic switch (attackState) { case (AttackState.ATTACKING): if (attackTimer > 0) { attackTimer -= Time.deltaTime; } else { attackTrigger.enabled = false; attackState = AttackState.NOT_ATTACKING; } break; case (AttackState.NOT_ATTACKING): if (Input.GetKeyDown(KeyCode.F)) { attackTimer = attackDuration; attackTrigger.enabled = true; attackState = AttackState.ATTACKING; } break; default: break; } // Attack animation anim.SetBool("Attacking", attackState == AttackState.ATTACKING); }