fgm24/Assets/Scripts/Player/PlayerMovement.cs

105 lines
2.8 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;
2024-02-03 15:21:45 +01:00
using System.Linq;
2024-02-02 22:43:39 +01:00
2024-02-03 14:07:03 +01:00
public class PlayerMovement : MonoBehaviour
2024-02-02 22:43:39 +01:00
{
2024-02-03 14:07:03 +01:00
public int player = 0;
2024-02-02 22:43:39 +01:00
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;
2024-02-03 14:07:03 +01:00
[Header("Whipping")]
[SerializeField]
RopeWhipAttack whipAttack;
[SerializeField]
private float whipMoveSpeed = 25f;
[SerializeField]
private float maxWhipMoveSpeed = 30f;
2024-02-02 22:43:39 +01:00
private void Start()
{
rb = GetComponent<Rigidbody2D>();
2024-02-03 15:21:45 +01:00
playerController = Gamepad.all.ElementAtOrDefault(player);
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()
{
2024-02-03 14:07:03 +01:00
if (whipAttack.IsBeingWhipped)
{
2024-02-03 15:03:38 +01:00
//float sign = Vector2.Dot((whipAttack.joint.position - whipAttack.otherPlayerAttack.joint.position).normalized, movement.normalized);
2024-02-03 14:07:03 +01:00
Vector2 ropeDir = whipAttack.otherPlayerAttack.joint.position - whipAttack.joint.position;
Vector2 tangent = new Vector2(-ropeDir.y, ropeDir.x).normalized;
2024-02-03 15:03:38 +01:00
rb.AddForce(Vector2.Dot(movement, tangent) * tangent * whipMoveSpeed);
2024-02-03 14:07:03 +01:00
rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxWhipMoveSpeed);
}
else if (whipAttack.IsWhippingOtherPlayer)
{
movement = Vector2.zero;
}
else
{
rb.AddForce(movement * moveSpeed);
}
2024-02-02 22:43:39 +01:00
}
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)
{
2024-02-03 15:21:45 +01:00
rumble.GetComponent<RumbleManager>().RumblePulse(0.0f, 0.004f, stepVibrationTime, player);
2024-02-02 22:43:39 +01:00
right = false;
}
else if (!right)
{
2024-02-03 15:21:45 +01:00
rumble.GetComponent<RumbleManager>().RumblePulse(0.004f, 0.0f, stepVibrationTime, player);
2024-02-02 22:43:39 +01:00
right = true;
}
vibrate = false;
}
}
private IEnumerator ToggleWithDelay()
{
while (true)
{
vibrate = !vibrate;
yield return new WaitForSeconds(stepCooldown);
}
}
}