using System; using System.Collections.Generic; using UnityEngine; namespace Player { [RequireComponent(typeof(Rigidbody2D))] public class Movement : MonoBehaviour { [SerializeField] private float runSpeed; [SerializeField] private float springPower; [SerializeField] private SpriteRenderer spriteRenderer; [SerializeField] private float turnDelay; private bool _onSpring; private Rigidbody2D _rb; private bool _grounded = true; private bool _facingLeft; private float _turnDelayTimer; private void Awake() { _rb = GetComponent(); } private void Update() { _turnDelayTimer += Time.deltaTime; spriteRenderer.flipX = !_facingLeft; } private void FixedUpdate() { if (_grounded) { _rb.linearVelocityX = _facingLeft ? -runSpeed : runSpeed; } } private void OnCollisionStay2D(Collision2D other) { if (_turnDelayTimer < turnDelay) return; var contacts = new List(); other.GetContacts(contacts); foreach(var c in contacts) { var x = c.normal.x; if (Mathf.Abs(x) > 0.1f) { _facingLeft = !_facingLeft; _turnDelayTimer = 0f; return; } } } } }