| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class PlayerMovement : MonoBehaviour
- {
- [Header("Movement Settings")]
- public float moveSpeed = 5f;
- public float rotationSpeed = 10f;
- private Vector2 moveInput;
- private PlayerInput playerInput;
- private Rigidbody rb;
- void Awake()
- {
- playerInput = GetComponent<PlayerInput>();
- }
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- }
- public void OnMove(InputAction.CallbackContext context)
- {
- moveInput = context.ReadValue<Vector2>();
- }
- void FixedUpdate()
- {
- HandleMovement();
- }
- void HandleMovement()
- {
- if (moveInput.magnitude > 0.1f)
- {
- Vector3 movement = new Vector3(moveInput.x, 0, moveInput.y) * moveSpeed;
- rb.linearVelocity = new Vector3(movement.x, rb.linearVelocity.y, movement.z);
- Vector3 lookDirection = new Vector3(moveInput.x, 0, moveInput.y);
- if (lookDirection != Vector3.zero)
- {
- Quaternion targetRotation = Quaternion.LookRotation(lookDirection);
- transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
- }
- }
- }
- }
|