using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(CharacterController))]
public class VRMoveCustom : MonoBehaviour
{
[Header("Движение")]
[SerializeField] private float moveSpeed = 3f;
[Header("Поворот")]
[SerializeField] private float turnSpeed = 90f;
[Header("Ввод")]
[SerializeField] private InputActionReference moveAction;
[SerializeField] private InputActionReference turnAction;
private CharacterController _charController;
private Vector3 _velocity;
private void Awake()
{
_charController = GetComponent<CharacterController>();
EnableInput();
}
private void OnEnable() => EnableInput();
private void OnDisable() => DisableInput();
private void EnableInput()
{
if (moveAction?.action != null && !moveAction.action.enabled)
moveAction.action.Enable();
if (turnAction?.action != null && !turnAction.action.enabled)
turnAction.action.Enable();
}
private void DisableInput()
{
if (moveAction?.action != null && moveAction.action.enabled)
moveAction.action.Disable();
if (turnAction?.action != null && turnAction.action.enabled)
turnAction.action.Disable();
}
void Start()
{
_charController.height = 1.8f;
_charController.radius = 0.3f;
_charController.center = new Vector3(0, 0.9f, 0);
}
void FixedUpdate()
{
Vector2 moveInput = moveAction.action?.ReadValue<Vector2>() ?? Vector2.zero;
Vector2 turnInput = turnAction.action?.ReadValue<Vector2>() ?? Vector2.zero;
Vector3 moveDirection = transform.forward * moveInput.y + transform.right * moveInput.x;
moveDirection.Normalize();
_velocity.x = moveDirection.x * moveSpeed;
_velocity.z = moveDirection.z * moveSpeed;
if(Mathf.Abs(turnInput.x)> 0.1f)
{
float deltaTurn = turnInput.x * turnSpeed * Time.deltaTime;
transform.Rotate(0, deltaTurn, 0);
}
if (_charController.isGrounded)
{
_velocity.y = 0f;
}
else
{
_velocity.y += -9.81f * Time.deltaTime;
}
_charController.Move(_velocity * Time.deltaTime);
}
}