fgm24/Assets/Scripts/Player/PlayerMovement1.cs

75 lines
1.9 KiB
C#
Raw Normal View History

2024-02-02 22:43:39 +01:00
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<Rigidbody2D>();
2024-02-03 12:42:00 +01:00
playerController = Gamepad.all[0];
2024-02-02 22:43:39 +01:00
StartCoroutine(ToggleWithDelay());
}
void Update()
{
2024-02-03 11:06:27 +01:00
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"); ;
}
2024-02-02 22:43:39 +01:00
if (movement.x != 0 || movement.y != 0)
RumbleWalk();
}
private void FixedUpdate()
{
rb.velocity = (movement * moveSpeed);
}
private void RumbleWalk()
{
2024-02-03 12:21:41 +01:00
if (vibrate && playerController != null)
2024-02-02 22:43:39 +01:00
{
if (right)
{
rumble.GetComponent<RumbleManager>().RumblePulse1(0.0f, 0.004f, stepVibrationTime);
right = false;
}
else if (!right)
{
rumble.GetComponent<RumbleManager>().RumblePulse1(0.004f, 0.0f, stepVibrationTime);
right = true;
}
vibrate = false;
}
}
private IEnumerator ToggleWithDelay()
{
while (true)
{
vibrate = !vibrate;
yield return new WaitForSeconds(stepCooldown);
}
}
}