简体   繁体   English

飞机 - 防止旋转超出限制,但允许转弯

[英]Aeroplane - prevent rotation outside limits, but allow turning

I have an aeroplane object that rotates using Input.GetAxis input.我有一架使用 Input.GetAxis 输入旋转的飞机 object。 It's limited to a 100degree range of motion on both z and y axes:它被限制在 z 和 y 轴上的 100 度运动范围内:

if (transform.eulerAngles.z < 50 || transform.eulerAngles.z > 310) {
  transform.Rotate(0f, Input.GetAxis("Horizontal"), -Input.GetAxis("Horizontal"));
}
if (transform.eulerAngles.y < 50 || transform.eulerAngles.y > 310) {
  transform.Rotate(Input.GetAxis("Vertical"), 0f, 0f);
}

The plane is propelled forward by pressing a button, and forward is the direction the plane is facing:飞机通过按下按钮向前推进, forward是飞机面对的方向:

transform.position += transform.forward * Time.deltaTime * speed;

This doesn't behave as I'd like.这不像我想要的那样。 When the plane reaches the limit of the rotation (eg transform.eulerAngles.z == 50), the plane no longer moves in an 'arc', as in, forward is not a straight line.当平面达到旋转极限时(例如 transform.eulerAngles.z == 50),平面不再以“弧”移动,例如, forward不是直线。 When it reaches the limit, the plane just moves in a straight line.当它到达极限时,飞机只是直线运动。 I'd like it to continue to arc, just not beyond the rotation limit.我希望它继续弧线,只是不超过旋转限制。 I hope that makes sense?我希望这是有道理的? Does anyone have any tips on how I can achieve this?有人对我如何实现这一目标有任何提示吗?

Your problem is that you are comparing the rotated angle with the euler angles.您的问题是您正在将旋转角度与欧拉角进行比较。 So, when you arrive to 50 degrees it is not posible to rotate more.因此,当您到达 50 度时,不可能再旋转更多。

You have to compare the rotation with the forward vector and limit this rotation to 50 degrees您必须将旋转与前向矢量进行比较并将此旋转限制为 50 度

I can't quite believe how obvious the solution was... Of course, I don't want the y axis to stop rotating.我不敢相信解决方案有多明显……当然,我不希望 y 轴停止旋转。 Here's my solution:这是我的解决方案:

float getHorizontal;
float getVertical;

if (transform.eulerAngles.z < 50 || transform.eulerAngles.z > 310) {
  getHorizontal = Input.GetAxis("Horizontal");
} else {
  getHorizontal = 0f;
}

if (transform.eulerAngles.x < 50 || transform.eulerAngles.x > 310) {
  getVertical = Input.GetAxis("Vertical");
} else {
  getVertical = 0f;
}

transform.Rotate(getVertical, Input.GetAxis("Horizontal"), -getHorizontal);

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

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