Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HTML
- // You'll need a button or key event to trigger the sprinting mechanic.
- // JavaScript
- const player = {
- speed: 5, // Normal movement speed
- sprintMultiplier: 2, // Multiplier applied to speed during sprint
- isSprinting: false, // Flag to track sprinting state
- };
- // Function to handle sprinting mechanic
- function handleSprinting() {
- // Check if the player is sprinting
- if (player.isSprinting) {
- player.speed = player.speed * player.sprintMultiplier;
- console.log("Player is sprinting");
- } else {
- player.speed = 5; // Reset speed to normal when not sprinting
- console.log("Player is not sprinting");
- }
- }
- // Event listener to toggle sprinting
- document.addEventListener("keydown", function(event) {
- if (event.key === "Shift") {
- player.isSprinting = true;
- handleSprinting();
- }
- });
- document.addEventListener("keyup", function(event) {
- if (event.key === "Shift") {
- player.isSprinting = false;
- handleSprinting();
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment