using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using System.Linq; public class PlayerMovement : MonoBehaviour { public int player = 0; 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; [Header("Whipping")] [SerializeField] RopeWhipAttack whipAttack; [SerializeField] private float whipMoveSpeed = 25f; [SerializeField] private float maxWhipMoveSpeed = 30f; private void Start() { rb = GetComponent(); playerController = Gamepad.all.ElementAtOrDefault(player); 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() { if (whipAttack.IsBeingWhipped) { //float sign = Vector2.Dot((whipAttack.joint.position - whipAttack.otherPlayerAttack.joint.position).normalized, movement.normalized); Vector2 ropeDir = whipAttack.otherPlayerAttack.joint.position - whipAttack.joint.position; Vector2 tangent = new Vector2(-ropeDir.y, ropeDir.x).normalized; rb.AddForce(Vector2.Dot(movement, tangent) * tangent * whipMoveSpeed); rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxWhipMoveSpeed); } else if (whipAttack.IsWhippingOtherPlayer) { movement = Vector2.zero; } else { rb.AddForce(movement * moveSpeed); } } private void RumbleWalk() { if (vibrate && playerController != null) { if (right) { rumble.GetComponent().RumblePulse(0.0f, 0.004f, stepVibrationTime, player); right = false; } else if (!right) { rumble.GetComponent().RumblePulse(0.004f, 0.0f, stepVibrationTime, player); right = true; } vibrate = false; } } private IEnumerator ToggleWithDelay() { while (true) { vibrate = !vibrate; yield return new WaitForSeconds(stepCooldown); } } }