2012-10-11 4 views
3

Ive가 sidescroller shooter 게임을 수행하고 있습니다.LookAt (x, Vector3.Right)와 마찬가지로 slerp가 작동합니다.

chest.LookAt(mousepos, Vector3.right); 

&

chest.LookAt(mousepos, Vector3.left); 

(왼쪽 문자가 문자를 오른쪽으로 돌리면 왼쪽과 오른쪽으로 켜져있을 때)

을 : 필자는 다음에 주위를 둘러 보면 내 횡 스크롤 슈팅 게임 캐릭터를 가지고

그래서 다른 축을 목표로하지는 않겠지 만 ... 마우스가 캐릭터의 중간을 가로 지르지 않고 주변을 가로 지르지 않으면 프레임 사이의 작은 이동을 만드는 회전을 얻습니다. .. 그것의 코르에 그냥 텔레포트 할거야. LookAt와 같은 직사각형 회전.

글쎄요, 문제는 LookAt (x, Vector3.Right)와 똑같이 작동하는 Time.deltTime과 함께 작동하는 쿼터니언 슬래프를 얻는 방법입니다. 나는 Vector3.right를 가져야하고 왼쪽으로 가며 1 축을 움직일 것이다.

나를 도와 주신 모든 분들께 감사드립니다. :)

답변

1

나는 대상 벡터와 시작 벡터를 가질 것이고, 사용자가 마우스를 캐릭터의 왼쪽이나 오른쪽으로 가져 가면 업데이트 기능에서 그들 사이에 Vector3.Slerp을 사용할 것이다.

bool charWasFacingLeftLastFrame = false. 
Vector3 targetVector = Vector3.zero; 
Vector3 startVector = Vector3.zero; 
float startTime = 0f; 
float howLongItShouldTakeToRotate = 1f; 

void Update() { 
    Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 
    Vector3 characterPos = character.transform.position; 

    //check if mouse is left or right of character 
    if(mouseWorldPos.x < characterPos.x) { 
     //char should be facing left 
     //only swap direction if the character was facing the other way last frame  
     if(charWasFacingLeftLastFrame == false) { 
      charWasFacingLeftLastFrame = true; 
      //we need to rotate the char 
      targetVector = Vector3.left; 
      startVector = character.transform.facing; 
      startTime = Time.time; 
     } 
    } else { 
     //char should be facing right 
     //only swap direction if the character was facing the other way last frame  
     if(charWasFacingLeftLastFrame == true) { 
      //we need to rotate the char; 
      charWasFacingLeftLastFrame = false 
      targetVector = Vector3.right; 
      startVector = character.transform.facing; 
      startTime = Time.time; 
     } 
    } 

    //use Slerp to update the characters facing 
    float t = (Time.time - startTime)/howLongItShouldTakeToRotate; 
    character.transform.facing = Vector3.Slerp(startVector, targetVector, t); 
}