using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement1 : MonoBehaviour { public float moveSpeed = 5f; private Rigidbody2D rb; private Vector2 movement; private Gamepad playerController; private bool right = false; private bool vibrate = false; [SerializeField] private float stepCooldown = 0.05f; [SerializeField] private float stepVibrationTime = 0.05f; [SerializeField] private GameObject rumble; private void Start() { rb = GetComponent(); playerController = Gamepad.all[0]; StartCoroutine(ToggleWithDelay()); } void Update() { if (playerController != null) { movement.x = playerController.leftStick.x.ReadValue(); movement.y = playerController.leftStick.y.ReadValue(); } else { movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); ; } if (movement.x != 0 || movement.y != 0) RumbleWalk(); } private void FixedUpdate() { rb.velocity = (movement * moveSpeed); } private void RumbleWalk() { if (vibrate) { if (right) { rumble.GetComponent().RumblePulse1(0.0f, 0.004f, stepVibrationTime); right = false; } else if (!right) { rumble.GetComponent().RumblePulse1(0.004f, 0.0f, stepVibrationTime); right = true; } vibrate = false; } } private IEnumerator ToggleWithDelay() { while (true) { vibrate = !vibrate; yield return new WaitForSeconds(stepCooldown); } } }