using UnityEngine;

public class PlyaerController : MonoBehaviour
{
    int speed = 10; //스피드 

    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        Vector2 currentPosition = transform.position;

        // x좌표 제한 검사
        if (currentPosition.x >= 2.27f && h > 0)
        {
            h = 0; // 오른쪽 이동 제한
        }
        else if (currentPosition.x <= 2.29f && h < 0)
        {
            h = 0; // 왼쪽 이동 제한
        }

        // y좌표 제한 검사
        if (currentPosition.y >= 4.4f && v > 0)
        {
            v = 0; // 위쪽 이동 제한
        }
        else if (currentPosition.y <= -4.49f && v < 0)
        {
            v = 0; // 아래쪽 이동 제한
        }

        float xMove = h * speed * Time.deltaTime;
        float yMove = v * speed * Time.deltaTime;

        this.transform.Translate(new Vector2(xMove, yMove));
    }
}

반응형

+ Recent posts