using System.Globalization;
using Unity.Netcode;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class OVRStickMovement : NetworkBehaviour
{
    public float speed = 2.0f;
    public float gravity = -9.81f;
    public Transform cameraTransform;

    private CharacterController characterController;
    private Vector3 velocity;

    void Start()
    {
        if (!IsOwner)
        {
            Debug.LogError("Ja, ich bin Owner!");
            return;
        }
        characterController = GetComponent<CharacterController>();

        if (cameraTransform == null && Camera.main != null)
            cameraTransform = Camera.main.transform;
    }

    void Update()
    {
        if(!IsOwner) return;
        // Linker Stick auslesen (PrimaryThumbstick)
        Vector2 input = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick);

        // Bewegung relativ zur Kameraausrichtung
        Vector3 move = cameraTransform.forward * input.y + cameraTransform.right * input.x;
        move.y = 0; // keine vertikale Bewegung
        move.Normalize();

        characterController.Move(move * speed * Time.deltaTime);

        // Gravity anwenden
        velocity.y += gravity * Time.deltaTime;
        characterController.Move(velocity * Time.deltaTime);

        // Reset, wenn auf dem Boden
        if (characterController.isGrounded)
        {
            velocity.y = 0f;
        }
    }
}
