using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 10.0f;
    private Vector3 position;

    void Start()
    {
        // Initialize the position to the current position of the GameObject
        position = transform.position;
    }

    void Update()
    {
        try
        {
            // Capture user input
            float moveHorizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
            float moveVertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;

            // Move the GameObject
            position.x += moveHorizontal;
            position.y += moveVertical;

            // Update the GameObject's position
            transform.position = position;

            // Debug statement to track position
            Debug.Log("Current position: " + position);

            // Simulate an error (e.g., out-of-bounds) for demonstration
            if (position.x > 100 || position.x < -100 || position.y > 100 || position.y < -100)
            {
                throw new System.IndexOutOfRangeException("Position out of bounds!");
            }
        }
        catch (System.NullReferenceException nullEx)
        {
            // Handle null reference exception
            Debug.LogError("Null reference exception: " + nullEx.Message);
        }
        catch (System.IndexOutOfRangeException indexEx)
        {
            // Handle out-of-bounds exception
            Debug.LogError("Index out of range exception: " + indexEx.Message);
        }
        catch (System.Exception ex)
        {
            // Handle any other exceptions
            Debug.LogError("An error occurred: " + ex.Message);
        }
    }
}