fgm24/Assets/Scripts/Player/PlayerMovement2.cs

68 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement2 : 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 float cooldown;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
playerController = Gamepad.all[1];
StartCoroutine(ToggleWithDelay());
}
void Update()
{
movement.x = playerController.leftStick.x.ReadValue();
movement.y = playerController.leftStick.y.ReadValue();
if (movement.x != 0 || movement.y != 0)
RumbleWalk();
}
private void FixedUpdate()
{
rb.velocity = (movement * moveSpeed);
}
private void RumbleWalk()
{
if (vibrate)
{
if (right)
{
rumble.GetComponent<RumbleManager>().RumblePulse2(0.0f, 0.004f, stepVibrationTime);
right = false;
}
else if (!right)
{
rumble.GetComponent<RumbleManager>().RumblePulse2(0.004f, 0.0f, stepVibrationTime);
right = true;
}
vibrate = false;
}
}
private IEnumerator ToggleWithDelay()
{
while (true)
{
vibrate = !vibrate;
yield return new WaitForSeconds(stepCooldown);
}
}
}