【问题标题】:Get slerp to work just as LookAt(x,Vector3.Right) does让 slerp 像 LookAt(x,Vector3.Right) 一样工作
【发布时间】:2012-10-02 07:19:21
【问题描述】:

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

chest.LookAt(mousepos, Vector3.right);

&

chest.LookAt(mousepos, Vector3.left);

(左转时为左,右转时为右)

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

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

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

【问题讨论】:

    标签: transform unity3d quaternions side-scroller


    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 2013-10-14
      • 2022-01-11
      • 2016-01-14
      • 2011-06-18
      相关资源
      最近更新 更多