본문 바로가기
카테고리 없음

[Unity 2D / 유니티 교과서] 5장 예제 + 이동범위 제한

by 왹져박사 2023. 1. 31.
728x90

Mathf.Clamp(value, min, max)를 사용하여 x좌표 이동범위 제한

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float radius;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float posX = Mathf.Clamp(transform.position.x, -8.0f, 8.0f);

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            this.transform.Translate(-3, 0, 0);
        }
        else if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            this.transform.Translate(3, 0, 0);
        }
        transform.position = new Vector3(posX, -3.78f, 0);

    }

    public void LButtonDown()
    {
        this.transform.Translate(-3, 0, 0);
    }
    public void RButtonDown()
    {
        this.transform.Translate(3, 0, 0);
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(this.transform.position, 1f);
    }
}

 

 

728x90