[英]Don't rotate z-axis while rotating around Vector3.up and Vector3.left
I have a Space Shuttle that i want to rotate
on Vector3.up
and Vector3.left
, so that the player can look up/down and left/right with the Shuttle. 我有一个要在Vector3.up
和Vector3.left
上rotate
Vector3.left
,以便玩家可以使用Shuttle向上/向下和向左/向右查找。
This is the code i use: 这是我使用的代码:
// Rotate the ship
transform.Rotate(Vector3.up, distX * rotateSpeed * Time.deltaTime, Space.Self);
transform.Rotate(Vector3.left, distY * rotateSpeed * Time.deltaTime, Space.Self);
where distX
is a value for the strength of the rotation, based on Mouse X
- Axis and rotateSpeed
is just a value for the speed of the rotation. 其中distX
是旋转强度的值,基于Mouse X
-Axis, rotateSpeed
只是旋转速度的值。
However, if i use this script to rotate my ship, it gets rotated by the z-axis, too. 但是,如果我使用此脚本旋转我的船,那么它也会通过z轴旋转。
And i have no idea why it's doing this. 而且我不知道为什么要这么做。 These are the only lines in my code (yet) that do something with the rotation of the Shuttle. 这是我的代码中(到目前为止)仅有的几行与Shuttle的旋转有关的事情。
What i tried yet is to replace Vector3.up
by transform.up
and Vector3.left
by transform.right
(also changed distY
to -distY
in the second case), but didn't work either. 我试过没是更换Vector3.up
由transform.up
和Vector3.left
通过transform.right
(也改变distY
到-distY
在第二种情况下),但也不能工作。
You can assign Euler value of 0 to z like which allows you to restrict rotation in z axis : 您可以将z的Euler值指定为0,这样可以限制z轴的旋转:
distX = Input.GetAxis("Vertical") * rotateSpeed * Time.deltaTime;
distY = Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;
transform.rotation = Quaternion.Euler(distX, distY, 0f);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.