2012-10-11 42 views
3

我一直在研究sidescroller射擊遊戲。香港專業教育學院得到了我的sidescroller射手性格與這些環顧四周:讓斯萊普工作就像LookAt(x,Vector3.Right)一樣

chest.LookAt(mousepos, Vector3.right); 

&

chest.LookAt(mousepos, Vector3.left); 

(左當一個字符轉向左邊和右邊當一個字符向右轉)

所以它不會瞄準任何其他軸......但是當鼠標穿過角色的中間而不是圍繞它時,它會獲得旋轉以在幀之間進行小型運輸,並且它不會一直到達那裏它只會傳送到它的公司rect旋轉像LookAt應該。

那麼問題是,我如何得到任何與Time.deltTime一起工作的四元數slerp與LookAt(x,Vector3.Right)一樣工作?我必須有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); 
} 
相關問題