키보드로 오브젝트를 이동시 좌우나 앞으로의 이동을 화면 안으로 제한하고 싶을 경우 쓰는 스크립트
Camera.main.WorldToScreenPoint함수의
Camera.main의 의미는 태그가 mainCamera로 설정된 카메라를 의미합니다.
메인카메라를 기준으로 하는 월드 좌표를 스크린 좌표로 변행해주는 함수 입니다.
if문에서는 왼쪽으로 이동시(key < 0 ) 위치를 제한( pos.x > 0) 해주고 오른쪽으로 이동시(key > 0 ) 위치를 제한(pos.x < Screen.width)해주고 있습니다.
이와 같이 오브젝트의 위치를 스크린 좌표로 변환하면 화면 크기에 상관없게 오브젝트의 위치를 제한 할 수 있습니다.
Camera.WorldToScreenPoint
Description
Transforms position from world space into screen space.
Screenspace is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight). The z position is in world units from the camera.
using UnityEngine; using System.Collections; public class Example : MonoBehaviour { public Transform target; void Update() { Vector3 screenPos = camera.WorldToScreenPoint(target.position); print("target is " + screenPos.x + " pixels from the left"); } }
'유니티 > 스크립트' 카테고리의 다른 글
| 폭파 불꽃 관련 파티클 이미지 (0) | 2014.01.28 |
|---|---|
| 유니티의 오브젝트 크기, 위치, 색 설정하기 (0) | 2014.01.28 |
| 유니티 WorldToScreenPoint를 사용하여 좌우 이동을 제한하는 스크립트 (0) | 2014.01.28 |
| 유니티 오브젝트 키보드로 움직이기 (0) | 2014.01.28 |
| 배경화면 스크롤 방법 (0) | 2014.01.28 |
| 제트기 뒤 분화구 파티클 패키지 파일 (0) | 2014.01.28 |
Trackback 0 And
Comment 0


