简体   繁体   中英

Aeroplane - prevent rotation outside limits, but allow turning

I have an aeroplane object that rotates using Input.GetAxis input. It's limited to a 100degree range of motion on both z and y axes:

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:

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. 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.

You have to compare the rotation with the forward vector and limit this rotation to 50 degrees

I can't quite believe how obvious the solution was... Of course, I don't want the y axis to stop rotating. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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