화면 끝에서 다른 쪽 끝으로 이동

2022. 6. 16. 22:27Unity/스크립트 응용

public float leftConstraint = 0.0f;
  public float rightConstraint = 0.0f;
  public float buffer = 1.0f; // set this so the spaceship disappears offscreen before re-appearing on other side
  public float distanceZ = 10.0f;
 
  void Awake() 
  {
      // set Vector3 to ( camera left/right limits, spaceship Y, spaceship Z )
      // this will find a world-space point that is relative to the screen
 
      // using the camera's position from the origin (world-space Vector3(0,0,0)
      //leftConstraint = Camera.main.ScreenToWorldPoint( new Vector3(0.0f, 0.0f, 0 - Camera.main.transform.position.z) ).x;
      //rightConstraint = Camera.main.ScreenToWorldPoint( new Vector3(Screen.width, 0.0f, 0 - Camera.main.transform.position.z) ).x;
  
      // using a specific distance
      leftConstraint = Camera.main.ScreenToWorldPoint( new Vector3(0.0f, 0.0f, distanceZ) ).x;
      rightConstraint = Camera.main.ScreenToWorldPoint( new Vector3(Screen.width, 0.0f, distanceZ) ).x;
  }
 
  void Update() 
  {
 
      if (transform.position.x < leftConstraint - buffer) { // ship is past world-space view / off screen
         transform.position=  new Vector3(rightConstraint + buffer,transform.position.y, transform.position.z);  // move ship to opposite side
      }
 
      if (transform.position.x > rightConstraint + buffer) {
         transform.position = new Vector3(leftConstraint  - buffer,transform.position.y, transform.position.z);
      }
      
  }​
float leftConstraint = Screen.width;
     float rightConstraint = Screen.width;
     float bottomConstraint = Screen.height;
     float topConstraint = Screen.height;
     float buffer = 1.0f;
     Camera cam;
     float distanceZ;
 
     void Start() {
         cam = Camera.main;
         distanceZ = Mathf.Abs(cam.transform.position.z + transform.position.z);
 
         leftConstraint = cam.ScreenToWorldPoint(new Vector3(0.0f, 0.0f, distanceZ)).x;
         rightConstraint = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0.0f, distanceZ)).x;
         bottomConstraint = cam.ScreenToWorldPoint(new Vector3(0.0f, 0.0f, distanceZ)).y;
         topConstraint = cam.ScreenToWorldPoint(new Vector3(0.0f, Screen.height, distanceZ)).y;
     }
 
     void FixedUpdate() {
         if (transform.position.x < leftConstraint - buffer) {
             transform.position = new Vector3(rightConstraint + buffer, transform.position.y, transform.position.z);
         }
         if (transform.position.x > rightConstraint + buffer) {
             transform.position = new Vector3(leftConstraint - buffer, transform.position.y, transform.position.z);
         }
         if (transform.position.y < bottomConstraint - buffer) {
             transform.position = new Vector3(transform.position.x, topConstraint + buffer, transform.position.z);
         }
         if (transform.position.y > topConstraint + buffer) {
             transform.position = new Vector3(transform.position.x, bottomConstraint - buffer, transform.position.z);
         }
     }
728x90

'Unity > 스크립트 응용' 카테고리의 다른 글

mirror 네트워크  (0) 2022.09.11
카메라 비율 고정  (0) 2022.06.16
JSON 데이터 저장  (0) 2022.06.16