Butonlara basınca sürekli basılı kalıyor , aynı yönde hareket ediyor sürekli using…
- User
Butonlara basınca sürekli basılı kalıyor , aynı yönde hareket ediyor sürekli
using System; using UnityEngine; using UnityEngine.EventSystems; using Photon.Pun;
namespace TarodevController { [RequireComponent(typeof(Rigidbody2D), typeof(Collider2D), typeof(PhotonView))] public class PlayerController : MonoBehaviourPun, IPlayerController, IPunObservable, IPointerDownHandler, IPointerUpHandler { private Rigidbody2D _rb; private CapsuleCollider2D _col; private FrameInput _frameInput; private Vector2 _frameVelocity; private bool _cachedQueryStartInColliders;
private PhotonView _photonView; private bool _moveRight, _moveLeft, _jumpPressed; [Header("LAYERS")] public LayerMask PlayerLayer; [Header("INPUT")] public bool SnapInput = true; public float VerticalDeadZoneThreshold = 0.3f; public float HorizontalDeadZoneThreshold = 0.1f; [Header("MOVEMENT")] public float MaxSpeed = 14; public float Acceleration = 120; public float GroundDeceleration = 60; public float AirDeceleration = 30; public float GroundingForce = -2.5f; [Header("GROUNDING")] public float GrounderDistance = 0.05f; [Header("JUMP")] public float JumpPower = 36; public float MaxFallSpeed = 40; public float FallAcceleration = 110; public float JumpEndEarlyGravityModifier = 3; public float CoyoteTime = 0.15f; public float JumpBuffer = 0.2f; #region Interface public Vector2 FrameInput => _frameInput.Move; public event Action<bool, float> GroundedChanged; public event Action Jumped; #endregion private float _time; private void Awake() { _rb = GetComponent<Rigidbody2D>(); _col = GetComponent<CapsuleCollider2D>(); _photonView = GetComponent<PhotonView>(); _cachedQueryStartInColliders = Physics2D.queriesStartInColliders; } private void Update() { if (_photonView.IsMine) { _time += Time.deltaTime; GatherInput(); } } private void GatherInput() { // Buton inputlarına göre hareket değerlerini belirle _frameInput = new FrameInput { JumpDown = _jumpPressed, JumpHeld = _jumpPressed, Move = new Vector2(_moveRight ? 1 : _moveLeft ? -1 : 0, 0) }; // Zıplama tetiklendikten sonra bir sonraki frame için sıfırlanır _jumpPressed = false; if (SnapInput) { _frameInput.Move.x = Mathf.Abs(_frameInput.Move.x) < HorizontalDeadZoneThreshold ? 0 : Mathf.Sign(_frameInput.Move.x); } if (_frameInput.JumpDown) { _jumpToConsume = true; _timeJumpWasPressed = _time; } } private void FixedUpdate() { if (_photonView.IsMine) { CheckCollisions(); HandleJump(); HandleDirection(); HandleGravity(); ApplyMovement(); } } #region Collisions private float _frameLeftGrounded = float.MinValue; private bool _grounded; private void CheckCollisions() { Physics2D.queriesStartInColliders = false; bool groundHit = Physics2D.CapsuleCast(_col.bounds.center, _col.size, _col.direction, 0, Vector2.down, GrounderDistance, ~PlayerLayer); if (groundHit && _frameVelocity.y <= 0.01f) { if (!_grounded) { _grounded = true; _coyoteUsable = true; _bufferedJumpUsable = true; _endedJumpEarly = false; GroundedChanged?.Invoke(true, Mathf.Abs(_frameVelocity.y)); } _frameVelocity.y = GroundingForce; } else if (_grounded && !groundHit) { _grounded = false; _frameLeftGrounded = _time; GroundedChanged?.Invoke(false, 0); } Physics2D.queriesStartInColliders = _cachedQueryStartInColliders; } #endregion #region Jumping private bool _jumpToConsume; private bool _bufferedJumpUsable; private bool _endedJumpEarly; private bool _coyoteUsable; private float _timeJumpWasPressed; private bool HasBufferedJump => _bufferedJumpUsable && _time < _timeJumpWasPressed + JumpBuffer; private bool CanUseCoyote => _coyoteUsable && !_grounded && _time < _frameLeftGrounded + CoyoteTime; private void HandleJump() { if (!_endedJumpEarly && !_grounded && !_frameInput.JumpHeld && _rb.velocity.y > 0) _endedJumpEarly = true; if (!_jumpToConsume && !HasBufferedJump) return; if (_grounded || CanUseCoyote) ExecuteJump(); _jumpToConsume = false; } private void ExecuteJump() { _endedJumpEarly = false; _timeJumpWasPressed = 0; _bufferedJumpUsable = false; _coyoteUsable = false; _frameVelocity.y = JumpPower; Jumped?.Invoke(); } #endregion #region Horizontal private void HandleDirection() { if (_frameInput.Move.x == 0) { var deceleration = _grounded ? GroundDeceleration : AirDeceleration; _frameVelocity.x = Mathf.MoveTowards(_frameVelocity.x, 0, deceleration * Time.fixedDeltaTime); } else { _frameVelocity.x = Mathf.MoveTowards(_frameVelocity.x, _frameInput.Move.x * MaxSpeed, Acceleration * Time.fixedDeltaTime); } } #endregion #region Gravity private void HandleGravity() { if (_grounded && _frameVelocity.y <= 0f) { _frameVelocity.y = GroundingForce; } else { var inAirGravity = FallAcceleration; if (_endedJumpEarly && _frameVelocity.y > 0) inAirGravity *= JumpEndEarlyGravityModifier; _frameVelocity.y = Mathf.MoveTowards(_frameVelocity.y, -MaxFallSpeed, inAirGravity * Time.fixedDeltaTime); } } #endregion private void ApplyMovement() => _rb.velocity = _frameVelocity; public void OnPointerDown(PointerEventData eventData) { switch (eventData.pointerCurrentRaycast.gameObject.name) { case "ButtonRight": _moveRight = true; break; case "ButtonLeft": _moveLeft = true; break; case "ButtonJump": _jumpPressed = true; break; } } public void OnPointerUp(PointerEventData eventData) { switch (eventData.pointerCurrentRaycast.gameObject.name) { case "ButtonRight": _moveRight = false; break; case "ButtonLeft": _moveLeft = false; break; case "ButtonJump": _jumpPressed = false; break; } } public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.IsWriting) { stream.SendNext(transform.position); stream.SendNext(_rb.velocity); } else { transform.position = (Vector3)stream.ReceiveNext(); _rb.velocity = (Vector2)stream.ReceiveNext(); } } } public struct FrameInput { public bool JumpDown; public bool JumpHeld; public Vector2 Move; } public interface IPlayerController { event Action<bool, float> GroundedChanged; event Action Jumped; Vector2 FrameInput { get; } } - Assistant
- User
sağ butona basınca sürekli sağa gidiyor
- Assistant
- User
- Assistant
- User
çalışmıyor daha gelişmiş ve güzel olsun kodu baştan sonra yaz
- Assistant