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);
}
}