gpt4 book ai didi

transform - 让 slerp 像 LookAt(x,Vector3.Right) 一样工作

转载 作者:行者123 更新时间:2023-12-02 03:55:37 25 4
gpt4 key购买 nike

我一直在研究一款横版射击游戏。我让我的 sidescroller 射手角色用这些环顾四周:

chest.LookAt(mousepos, Vector3.right);

&

chest.LookAt(mousepos, Vector3.left);

(向左转为左,向右转为向右)

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

好吧,问题是,我如何让任何与 Time.deltTime 一起工作的四元数 slerp 与 LookAt(x,Vector3.Right) 一样工作?我必须有 Vector3.right 和 left,这样它才能通过 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);
}

关于transform - 让 slerp 像 LookAt(x,Vector3.Right) 一样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12842736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com