简体   繁体   English

玩家沿着错误的轴旋转?

[英]Player rotating along wrong axis?

I have a character that is essentially a sphere, but scaled at y1, x1.2, z1 (a spheroid). 我的角色本质上是一个球体,但缩放比例为y1,x1.2,z1(椭球体)。

I can apply force to the object in any given 'pushDirection', which is a product of the character's rigidbody position minus the main camera transform position (this works fine to move the character in the direction the camera is facing – 'forwards' in my game should always be in the direction the camera is facing). 我可以在任何给定的“ pushDirection”中向对象施加力,该“ pushDirection”是角色的刚体位置减去主摄影机变换位置的乘积(这可以使角色朝摄影机所面对的方向移动–在我的“向前”位置上效果很好游戏应该始终朝着相机所面对的方向)。

But now I'm trying to make the object rotate forwards – in the direction the camera is facing – instead of adding force. 但是现在,我试图使对象向前旋转 -朝照相机朝向的方向-而不是施加力。 I'm adding torque instead. 我要增加扭矩。 [Here's a rough illustrated screenshot of my goal with my intended axis: [下面是我用目标轴表示的目标的屏幕截图:

Note that whenever the camera rotates around the object, the axis for rotation will shift. 请注意,每当相机绕着物体旋转时,旋转轴就会移动。

Instead though, I'm getting a result where the character rotates as is shown in this illustrated screenshot: 但是,相反,我得到了一个字符旋转的结果,如下面的屏幕截图所示:

As mentioned, as the camera moves the axis for rotation shifts along with the camera. 如上所述,随着照相机的移动,旋转轴随照相机一起移动。 But I want the object to move away from the camera rather than rotate to the left. 但我希望该对象远离摄像机,而不是向左旋转。 (Note, in both screenshots I had the object suspended without gravity to try and debug its rotation). (请注意,在两个屏幕截图中,我都将对象悬空而没有重力以尝试调试其旋转)。

Relevant bits of my script are below: 我的脚本的相关位如下:

 void Start () { rb = GetComponent<Rigidbody>(); pushForward = rb.transform.position - Camera.main.transform.position; } void Update() { speed = rb.velocity.magnitude; pushForward = rb.transform.position - Camera.main.transform.position; if (Input.GetKey("w")) { isForward = true; } else { isForward = false; } } void FixedUpdate () { // IF KEY IS DOWN, MOVE // if (isForward == true) { float turn = Input.GetAxis("Vertical"); rb.AddTorque(pushForward * torque * turn); Debug.Log("Moving Forward"); } } 

I've played around with this so much I just can't get my head around it. 我已经玩了这么多,我只是无法理解。 I'm also totally confused as if I change the Input.GetAxis to "Horizontal", the debug message "Moving Forward" pops up fine but I don't get any sort of rotation in the object – none at all. 我也很困惑,好像我将Input.GetAxis更改为“ Horizo​​ntal”,调试消息“ Moving Forward”弹出的很好,但是我没有在对象中进行任何旋转–根本没有旋转。

If anyone could point me in the right direction I'd really appreciate it, i've gotten myself a bit confused. 如果有人能指出正确的方向,我将不胜感激,这让我有些困惑。

If I understood you correctly, in order to obtain the rotation you want, you need to cross multiply pushForward with Vector3.down . 如果我对您的理解正确,那么为了获得所需的旋转角度,您需要将pushForwardVector3.down交叉。

IE: IE:

void FixedUpdate() {
    // IF KEY IS DOWN, MOVE //
    if (isForward == true) {
        var vect = Vector3.Cross(pushForward, Vector3.down);
        float turn = Input.GetAxis("Vertical");
        rb.AddTorque(vect * torque * turn);
        Debug.Log("Moving Forward");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM