fix чать инвенторя
This commit is contained in:
@@ -4,11 +4,11 @@ using UnityEngine;
|
||||
public class charecter : MonoBehaviour
|
||||
{
|
||||
[Header("Movement")]
|
||||
public float walkSpeed = 5f;
|
||||
public float runSpeed = 9f;
|
||||
public float walkSpeed = 5f;
|
||||
public float runSpeed = 9f;
|
||||
public float crouchSpeed = 2.5f;
|
||||
public float jumpHeight = 1.5f;
|
||||
public float gravity = -9.81f;
|
||||
public float jumpHeight = 1.5f;
|
||||
public float gravity = -9.81f;
|
||||
|
||||
[Header("References")]
|
||||
[Tooltip("Head BONE of the character.")]
|
||||
@@ -46,20 +46,21 @@ public class charecter : MonoBehaviour
|
||||
_cc = GetComponent<CharacterController>();
|
||||
_anim = GetComponentInChildren<Animator>();
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
|
||||
_bodyYaw = transform.eulerAngles.y;
|
||||
_headYaw = _bodyYaw;
|
||||
_headBindPose = head.localRotation;
|
||||
_headBindPose = head != null ? head.localRotation : Quaternion.identity;
|
||||
|
||||
// Ensure PlayerInput exists and subscribe to events
|
||||
// Убедиться что PlayerInput существует
|
||||
if (PlayerInput.Instance == null)
|
||||
{
|
||||
var go = new GameObject("PlayerInput");
|
||||
go.AddComponent<PlayerInput>();
|
||||
}
|
||||
|
||||
PlayerInput.Instance.OnPickupPressed += () => TryPickupNearbyItem();
|
||||
PlayerInput.Instance.OnJumpPressed += () => { _jumpRequested = true; };
|
||||
PlayerInput.Instance.OnPickupPressed += TryPickupNearbyItem;
|
||||
PlayerInput.Instance.OnJumpPressed += () => { _jumpRequested = true; };
|
||||
}
|
||||
|
||||
void Update()
|
||||
@@ -68,55 +69,35 @@ public class charecter : MonoBehaviour
|
||||
HandleMovement();
|
||||
}
|
||||
|
||||
// ── Item Pickup ───────────────────────────────────────────────────────────
|
||||
void HandlePickup()
|
||||
void LateUpdate()
|
||||
{
|
||||
// Input moved to PlayerInput events
|
||||
ApplyRotations();
|
||||
}
|
||||
|
||||
// ── Item Pickup ───────────────────────────────────────────────────────────
|
||||
void TryPickupNearbyItem()
|
||||
{
|
||||
float pickupRadius = 2.5f;
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, pickupRadius);
|
||||
|
||||
Debug.Log($"[Pickup] Checking {colliders.Length} colliders in radius {pickupRadius}");
|
||||
|
||||
WorldItem closestItem = null;
|
||||
float closestDist = float.MaxValue;
|
||||
|
||||
foreach (var col in colliders)
|
||||
{
|
||||
var worldItem = col.GetComponent<WorldItem>();
|
||||
if (worldItem != null)
|
||||
if (worldItem == null) continue;
|
||||
|
||||
float dist = Vector3.Distance(transform.position, worldItem.transform.position);
|
||||
if (dist < closestDist && dist <= worldItem.interactRange)
|
||||
{
|
||||
float dist = Vector3.Distance(transform.position, worldItem.transform.position);
|
||||
Debug.Log($"[Pickup] Found WorldItem: {worldItem.itemData?.itemName ?? "null"} at dist {dist}");
|
||||
if (dist < closestDist)
|
||||
{
|
||||
closestDist = dist;
|
||||
closestItem = worldItem;
|
||||
}
|
||||
closestDist = dist;
|
||||
closestItem = worldItem;
|
||||
}
|
||||
}
|
||||
|
||||
if (closestItem != null)
|
||||
{
|
||||
Debug.Log($"[Pickup] Trying to pick up: {closestItem.itemData?.itemName}");
|
||||
bool pickedUp = closestItem.TryPickupByButton();
|
||||
if (pickedUp)
|
||||
{
|
||||
Debug.Log($"Picked up: {closestItem.itemData.itemName}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("[Pickup] No WorldItem found nearby");
|
||||
}
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
ApplyRotations();
|
||||
closestItem.TryPickupByButton();
|
||||
}
|
||||
|
||||
// ── Mouse look ────────────────────────────────────────────────────────────
|
||||
@@ -125,21 +106,21 @@ public class charecter : MonoBehaviour
|
||||
var input = PlayerInput.Instance;
|
||||
if (input != null)
|
||||
{
|
||||
_headYaw += input.MouseX * mouseSensitivity;
|
||||
_pitch -= input.MouseY * mouseSensitivity;
|
||||
_headYaw += input.LookInput.x * mouseSensitivity;
|
||||
_pitch -= input.LookInput.y * mouseSensitivity;
|
||||
}
|
||||
_pitch = Mathf.Clamp(_pitch, -maxPitch, maxPitch);
|
||||
_pitch = Mathf.Clamp(_pitch, -maxPitch, maxPitch);
|
||||
}
|
||||
|
||||
// ── Movement ──────────────────────────────────────────────────────────────
|
||||
void HandleMovement()
|
||||
{
|
||||
var input = PlayerInput.Instance;
|
||||
float h = input != null ? input.Horizontal : Input.GetAxisRaw("Horizontal");
|
||||
float v = input != null ? input.Vertical : Input.GetAxisRaw("Vertical");
|
||||
float h = input != null ? input.MoveInput.x : 0f;
|
||||
float v = input != null ? input.MoveInput.y : 0f;
|
||||
|
||||
bool isCrouching = input != null ? input.Crouch : Input.GetKey(KeyCode.C);
|
||||
bool isRunning = input != null ? input.Run && !isCrouching : Input.GetKey(KeyCode.LeftShift) && !isCrouching;
|
||||
bool isCrouching = input != null && input.Crouch;
|
||||
bool isRunning = input != null && input.Run && !isCrouching;
|
||||
bool isMoving = (h * h + v * v) > 0.01f;
|
||||
|
||||
float speed = isCrouching ? crouchSpeed : isRunning ? runSpeed : walkSpeed;
|
||||
@@ -148,7 +129,7 @@ public class charecter : MonoBehaviour
|
||||
if (isMoving)
|
||||
_bodyYaw = Mathf.MoveTowardsAngle(_bodyYaw, _headYaw, bodyAlignSpeed * Time.deltaTime);
|
||||
|
||||
// Head-drag
|
||||
// Head-drag clamp
|
||||
float offset = Mathf.DeltaAngle(_bodyYaw, _headYaw);
|
||||
float clampedOffset = Mathf.Clamp(offset, -maxHeadYaw, maxHeadYaw);
|
||||
_bodyYaw += offset - clampedOffset;
|
||||
@@ -176,12 +157,11 @@ public class charecter : MonoBehaviour
|
||||
moveDir.y = _verticalVelocity;
|
||||
_cc.Move(moveDir * speed * Time.deltaTime);
|
||||
|
||||
// Animator params
|
||||
// Animator
|
||||
if (_anim != null)
|
||||
{
|
||||
// Speed: 0 = idle, 0.5 = walk, 1 = run
|
||||
float animSpeed = !isMoving ? 0f : isRunning ? 1f : 0.5f;
|
||||
_anim.SetFloat(HashSpeed, animSpeed, 0.1f, Time.deltaTime);
|
||||
_anim.SetFloat(HashSpeed, animSpeed, 0.1f, Time.deltaTime);
|
||||
_anim.SetBool (HashIsMoving, isMoving);
|
||||
_anim.SetBool (HashIsCrouching, isCrouching);
|
||||
_anim.SetBool (HashIsGrounded, _cc.isGrounded);
|
||||
@@ -193,10 +173,11 @@ public class charecter : MonoBehaviour
|
||||
{
|
||||
transform.rotation = Quaternion.Euler(0f, _bodyYaw, 0f);
|
||||
|
||||
float headOffset = Mathf.DeltaAngle(_bodyYaw, _headYaw);
|
||||
Quaternion yawRot = Quaternion.Euler(0f, headOffset, 0f);
|
||||
Quaternion pitchRot = Quaternion.Euler(_pitch, 0f, 0f);
|
||||
head.localRotation = _headBindPose * yawRot * pitchRot;
|
||||
if (head == null) return;
|
||||
|
||||
float headOffset = Mathf.DeltaAngle(_bodyYaw, _headYaw);
|
||||
Quaternion yawRot = Quaternion.Euler(0f, headOffset, 0f);
|
||||
Quaternion pitchRot = Quaternion.Euler(_pitch, 0f, 0f);
|
||||
head.localRotation = _headBindPose * yawRot * pitchRot;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user