2017-11-01 3 views
1

카메라가 내 플레이어를 따라 가지 않는 장면이 있습니다. 플레이어가 카메라 끝까지 도달하면 플레이어가 더 이상 갈 수 없기를 바란다. 어떻게해야합니까? 운동카메라 뷰 경계 내에서 2D 오브젝트를 이동하는 방법

public class PlayerBlueController : MonoBehaviour { 

public float speed; 
private float x; 



// Use this for initialization 
void Start() { 


} 

// Update is called once per frame 
void FixedUpdate() { 

    x = Input.GetAxis ("Horizontal")/100 * speed; 
    transform.Translate (x,0,0); 

} 
} 

에 대한

내 코드는 당신이에서 볼 수 있듯이. 그것은 카메라의 시야에서 벗어납니다. image

+0

두 개의 빈 게임 개체를 각 극단 모서리 (왼쪽 하단, 오른쪽 위)에 배치하십시오. 그런 다음 사용자 위치를 사용하여 사용자 이동을 고정시킵니다. 또는 당신이 마치 한 차원으로 움직이는 것처럼 보이면, x = Mathf.Clamp (x, minX, maxX); – Everts

답변

1

나는 당신이 Collider2D을 사용났습니다. transform.Translate 대신 Rigidbody2D.MovePosition을 사용해야합니다. 그렇지 않으면 transform.Translate을 사용하면 문제가 발생할 수 있습니다.

1 최종 이동 위치 .Take1의 결과로 Mathf.Clamp .Apply Camera.main.WorldToViewportPoint

2 한계와 ViewPortPoint 새로운 위치로 변환한다.

. 으로 ViewPortPoint를 다시 월드 지점으로 변환하십시오.

. 마지막으로 Rigidbody2D.MovePosition으로 이동하십시오.


아래 코드는 this에서 화면 경계에 대한 제한을 포함하도록 수정되었습니다.

이동Rigidbody없이 :

충돌 물리학이 필요하지 않은 경우에만 사용 :

public float speed = 100; 
public Transform obj; 

public void Update() 
{ 
    float h = Input.GetAxis("Horizontal"); 
    float v = Input.GetAxis("Vertical"); 

    //Move only if we actually pressed something 
    if ((h > 0 || v > 0) || (h < 0 || v < 0)) 
    { 
     Vector3 tempVect = new Vector3(h, v, 0); 
     tempVect = tempVect.normalized * speed * Time.deltaTime; 


     Vector3 newPos = obj.transform.position + tempVect; 
     checkBoundary(newPos); 
    } 
} 

void checkBoundary(Vector3 newPos) 
{ 
    //Convert to camera view point 
    Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos); 

    //Apply limit 
    camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f); 
    camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f); 

    //Convert to world point then apply result to the target object 
    obj.position = Camera.main.ViewportToWorldPoint(camViewPoint); 
} 

이동 개체Rigidbody2D과 : 충돌 물리학이 경우

사용 필수 :

public float speed = 100; 
public Rigidbody2D rb; 

public void Update() 
{ 
    float h = Input.GetAxis("Horizontal"); 
    float v = Input.GetAxis("Vertical"); 

    //Move only if we actually pressed something 
    if ((h > 0 || v > 0) || (h < 0 || v < 0)) 
    { 
     Vector3 tempVect = new Vector3(h, v, 0); 
     tempVect = tempVect.normalized * speed * Time.deltaTime; 

     //rb.MovePosition(rb.transform.position + tempVect); 

     Vector3 newPos = rb.transform.position + tempVect; 
     checkBoundary(newPos); 
    } 
} 

void checkBoundary(Vector3 newPos) 
{ 
    //Convert to camera view point 
    Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos); 

    //Apply limit 
    camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f); 
    camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f); 

    //Convert to world point then apply result to the target object 
    Vector3 finalPos = Camera.main.ViewportToWorldPoint(camViewPoint); 
    rb.MovePosition(finalPos); 
} 
+0

답변 해 주셔서 감사합니다. 정말 도움이되었습니다. 뭔가 물어보고 싶습니다. 정확히 mathf.clamp는 무엇을합니까? 왜냐하면 0.04f 0.96f 플레이어가 카메라의 한쪽면을 고집하는 수를 바꿀 때가 있기 때문입니다. 그 숫자들은 뭐야? 나는 그 뒤에있는 논리를 잡지 못했다. 그리고 운동에 관한 한 가지 질문. 버튼 플레이어를 놓으면 약간의 시간이 계속됩니다. 이 문제를 어떻게 방지 할 수 있습니까? – klavyeadam

+0

'Input.GetAxis' 대신'Input.GetAxisRaw'를 사용하여 즉시 멈추게하거나 'Rigidbody.velocity'를'Vector3.zero'로 설정하여 바로 중지하십시오. 화면 뷰포트는 x 축에서 0 (왼쪽)에서 1 (오른쪽) 사이의 값을 갖습니다 .'0.04f'는 객체가 x 축에 있도록하려는 최소값입니다. '0.96f'는 객체가 x 값에 있도록하려는 최대 값입니다. 공개 변수를 만들고 편집자에서 수정하여 화면상의 개체 위치에 대한 최소/최대 값을 결정하는 완벽한 값을 얻을 수 있습니다. – Programmer

+0

예를 들어,'public float min = 0.04f; 그리고'public float max = 0.96f;'camViewPoint.x = Mathf.Clamp (camViewPoint.x, min, max);'를 사용할 수 있습니다. 편집기에서 최소 및 최대 수정을 사용하여 원하는 완벽한 값을 얻을 수 있습니다. – Programmer

0

이미지가 응답하지 않습니다. 하지만 당신은 플레이어의 위치

x = Input.GetAxis ("Horizontal")/100 * speed; 
if(gameobject.transform.x > someValue) 
    x=0 

게임 오브젝트가 유 여기에 클래스를 첨부 장면에서 OBJECT됩니다 확인할 수 있습니다.

또 다른 방법은 장소를 invisibleWall 같은 입자 가속기 2 빈 게임 오브젝트를하고 플레이어에 입자 가속기를 얻을 수

0

가능한 해결 방법은 다음과 같습니다

화면 Cornes 보낸의 좌표 (왼쪽 위, 오른쪽 위, 왼쪽 아래하지 1.Get

, 오른쪽 하단). Screen.height and Screen.width을 사용하여이 좌표를 얻을 수 있습니다.

2. Camera.ScreenToWorldPoint으로 필요한 카메라를 사용하여이 좌표를 변환합니다.

3. 플레이어 좌표를 조정하고 화면 모서리의 4 좌표로 형성된 직사각형 안에 있는지 확인하십시오.

0

카메라의 가장자리를 기준으로 변형 위치를 제한해야합니다. Here is an answer describing the different coordinate systems in unity

당신은 아마 이런 식으로 뭔가를 찾고 :

float xMin = Camera.main.ViewportToWorldPoint(Vector3.zero).x; 
float xMax = Camera.main.ViewportToWorldPoint(Vector3.one).x; 

Vector3 currentPos = transform.position; 
float dx = Input.GetAxis ("Horizontal")/100 * speed; 
Vector3 desiredPos = new Vector3(currentPos.x + dx, currentPos.y, currentPos.z); 

Vector3 realPos = desiredPos; 

if(desiredPos.x > xMax) 
    realPos.x = xMax; 
else if(desiredPos.x < xMin) 
    realPos.x = xMin; 

transform.position = realPos; 

Read up here for more info on ViewportToWorldPoint(), it's extremely useful to become comfortable with the different coordinate spaces and how you can convert between them.

관련 문제