2012-10-11 32 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); 
} 
相关问题