2014-09-09 3 views
-1

내 게임 객체가 움직이는 방향을 향하도록 회전하려고합니다. 나는 리지드 바디를 사용하지 않고있다. 속도, 나는 단지 transform.position을 사용하여 물체를 움직인다. 여기 내 코드는 다음과 같습니다.2D 공간에서 회전 방향으로 회전 변환

public GameObject player; 

void Update() 
{ 
    Vector3 _origPos = new Vector3(player.transform.position.x,player.transform.position.y, player.transform.position.z); 

    if (Input.touchCount > 0) 
    { 
     // The screen has been touched so store the touch 
     Touch touch = Input.GetTouch(0); 
     if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) { 
      // If the finger is on the screen, move the object smoothly to the touch position 
      Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));     
      transform.position = Vector3.Lerp(transform.position, touchPosition, Time.deltaTime); 
      //transform.LookAt(new Vector3(touchPosition.x,touchPosition.y,0), Vector3.up); 
      Vector3 moveDirection = gameObject.transform.position - _origPos; 

     } 
    } 
} 

회전을 구현하는 방법에 대한 제안이 있으십니까? 나는 완전히 엉망진창이다

답변

0

player 게임 오브젝트 앞으로 직면 방향을 가정 할 작품이 내 2D 게임에서 나에게 원하는 효과를 얻었다 . 그것이

2

당신은 삼각법을 사용하여 그것을한다. 다행히 이미 3 점이 필요합니다. 필요

포인트 :

  • A) 이동할 것입니다.
  • B) 질량 중심의 현재 위치.
  • C) 개체의 선단, 앞.

B에서 C까지의 거리를 계산하여 x라고합니다. 그런 다음 B에서 A까지의 거리를 계산하여 y라고합니다.

각도를 찾으려면 역 탄젠트를 사용하십시오.

angle = arctan(y/x) 

그런 다음 개체 회전시 적용하십시오.

지역 대신 세계 좌표를 사용하십시오.

역 탄젠트에 Mathf을 사용하는 경우 라디안을 사용합니다.

+0

는 귀하의 의견에 감사드립니다 2D 축입니다에 머물 수 있도록

transform.LookAt(new Vector3(touchPosition.x,touchPosition.y,transform.position.z), Vector3.up); 

나는 객체 자신의 position.z 대신 touchPosition.z을 사용했다. 따라서 이미 Unity3D에는 이것을위한 사전 제작 방법이 없습니다. –

+0

그들은'LookAt()'http://docs.unity3d.com/ScriptReference/Transform.LookAt.html을 가지고 있지만 위의 제네릭 공식을 사용하기 위해 올바른 결과를 제공하지 않습니다. Mathf가 http://docs.unity3d.com/ScriptReference/Mathf.html을 도울 것입니다. – FunctionR

0

변형을 적용하는 데 Quaternion.LookRotation을 사용한다고 가정합니다.

뭔가 같은 :

Quaternion rotation = Quaternion.LookRotation(moveDirection); 
player.transform.rotation = rotation; 

이 당신의 코드를 빠르게 검사를하고 그 Z.