Unity에서 2D 게임을 만들고 있는데이 게임에서는 카메라를 움직일 필요가 없습니다. 따라서 플레이어의 변환을 기반으로하는 대신 충돌을 사용하여 카메라 경계 내에서 플레이어의 움직임을 제한하고 싶습니다. 솔직히 어디에서 이런 일을 시작해야할지 모르겠지만 스크립팅이 필요하다고 생각합니다. 나는이 시점에서 스크립팅에 꽤 익숙하지만, 당신의 대답에 스크립팅이 포함되어 있다면, 진행되고있는 모든 것에 대한 철저한 설명을 포함한다면 감사하겠습니다. By the by, 저는 C #을 사용하고 있습니다.
카메라가 직교 모드에있는 경우 EdgeCollider2D
를 사용 하여 화면 모서리의 세계 위치 ScreenToWorldPoint
를 찾은 다음을 사용 하여 EdgeCollider2D
.
유니티 커뮤니티 UnityLibrary Github에서는 이 예를 (아래 복사) :
// adds EdgeCollider2D colliders to screen edges
// only works with orthographic camera
using UnityEngine;
using System.Collections;
namespace UnityLibrary
{
public class ScreenEdgeColliders : MonoBehaviour
{
void Awake ()
{
AddCollider();
}
void AddCollider ()
{
if (Camera.main==null) {Debug.LogError("Camera.main not found, failed to create edge colliders"); return;}
var cam = Camera.main;
if (!cam.orthographic) {Debug.LogError("Camera.main is not Orthographic, failed to create edge colliders"); return;}
var bottomLeft = (Vector2)cam.ScreenToWorldPoint(new Vector3(0, 0, cam.nearClipPlane));
var topLeft = (Vector2)cam.ScreenToWorldPoint(new Vector3(0, cam.pixelHeight, cam.nearClipPlane));
var topRight = (Vector2)cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, cam.nearClipPlane));
var bottomRight = (Vector2)cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, 0, cam.nearClipPlane));
// add or use existing EdgeCollider2D
var edge = GetComponent<EdgeCollider2D>()==null?gameObject.AddComponent<EdgeCollider2D>():GetComponent<EdgeCollider2D>();
var edgePoints = new [] {bottomLeft,topLeft,topRight,bottomRight, bottomLeft};
edge.points = edgePoints;
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다