3

我一直在研究横向卷轴射击游戏。我已经让我的横向卷轴射击游戏角色环顾四周:

chest.LookAt(mousepos, Vector3.right);

&

chest.LookAt(mousepos, Vector3.left);

(角色向左转时为左,角色向右转时为右)

所以它不会瞄准任何其他轴...但是当鼠标越过角色的中间而不是围绕它时,它会旋转以在帧之间进行小型传输,并且不会一直到达那里..它只会像 LookAt 一样传送到正确的旋转。

那么问题是,如何让任何与 Time.deltTime 一起工作的四元数 slerp 与 LookAt(x,Vector3.Right) 一样工作?我必须拥有 Vector3.right 和 left,这样它才能通过 1 轴移动。

非常感谢任何帮助我的人。:)

4

1 回答 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);
}
于 2012-11-06T00:30:36.650 回答