67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
|
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>();
|
||
|
|
||
|
playerController = Gamepad.all[0];
|
||
|
|
||
|
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>().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);
|
||
|
}
|
||
|
}
|
||
|
}
|