Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vertex · Oct 13, 2010 at 05:16 PM · rotationlimitfly

simple plane code how to limit rotation

This is simple airplane code. I need to limit the z(yaw) and x(pitch) rotations. The following works (limits only z)-- like it runs without error, but doesn't work very well.
Is there a better way to do this? Please code examples would be very very much appreciated. I promise that I will learn from them.

var moveSpeed : float = 50; var yawSpeed : float = 90; var pitchSpeed : float = 70; //LEt's fly weeeee function Update () { transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime); transform.Rotate (-Vector3.forward * Input.GetAxis("Horizontal") * yawSpeed * Time.deltaTime); transform.Rotate (Vector3.right * Input.GetAxis("Vertical") * pitchSpeed * Time.deltaTime); }

//let's limit at least one angle so we can't do any crazy flying... var MinClamp = -10; var MaxClamp = 20;

function Start () { var angles = transform.rotation.eulerAngles; z= angles.z; }

 function LateUpdate(){

  transform.rotation.eulerAngles = new Vector3(
    transform.rotation.eulerAngles.x,
   transform.rotation.eulerAngles.y,
  Mathf.Clamp(transform.rotation.eulerAngles.z,MinClamp,MaxClamp));
  }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by skovacs1 · Oct 13, 2010 at 05:52 PM

You're on the right track, but what do you mean "doesn't work very well"? Could you please describe what you were expecting it to do and what it is not doing for you? Something like this should work to limit the axes:

var moveSpeed : float = 50; var pitchSpeed : float = 70; //up/down tilting var yawSpeed : float = 90; //left/right turning //var rollSpeed : float = 90; //left/right rolling var maxPitch : float = 40; var minPitch : float = -20; var maxYaw : float = 40;

private var eulers : Vector3 = Vector3.zero; //The current eulerAngles

function Start() { eulers = transform.eulerAngles; }

function Update() { //Rotate eulers.x += Input.GetAxis("Vertical") pitchSpeed Time.deltaTime; eulers.x = Mathf.Clamp(eulers.x, minPitch, maxPitch); eulers.z += Input.GetAxis("Horizontal") yawSpeed Time.deltaTime; eulers.z = Mathf.Clamp(eulers.y, -maxYaw, maxYaw); //eulers.y += Input.GetAxis("Lateral") rollSpeed Time.deltaTime; transform.eulerAngles = eulers;

 //Move
 transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);

}

Why would you need to limit both yaw and pitch? This forces the plane to always move in the frustum defined by your limits and therefore always move in the same general direction - Is this what you intended?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by vertex · Oct 13, 2010 at 08:47 PM

Shame for me. I got yaw rotation mixed up with roll z rotation. So to clarify now rotation about: x=pitch y=yaw z=roll

Having said that, I made some minor changes to the code that skovacs1 posted (to enable a roll) and it's getting closer to simple plane code with limited pitch and roll (no limit on yaw). There are two primary problems left: a) how to convert the roll angles into left and right yaw rotation so that the plane can be steered left and right-- original code simply used pitch to do this but required the plane to be tilted 90 degrees to ground (yes this was an oversight in my first post) The effect I'm trying to achieve is to limit the roll and simply have the turning speed left or right be a factor of the roll angle.

b) and inverted roll controls-- ie. hitting right arrow key brings the right wing up (rolls left)-- needs to roll right.

I've attempted both and come up a bit short. Note the last line. It does yaw the plane, but in a very spastic way.

Any help appreciated. I want to avoid physics save for collision.

var moveSpeed : float = 50;

var maxRoll : float = 40; var minRoll: float = -20; var rollSpeed : float = 20; //tilt left and right

var yawSpeed : float =60;//this is the turn left and right speed

var maxPitch : float = 40; var minPitch : float = -20; var pitchSpeed : float = 20; //up/down tilting

private var eulers : Vector3 = Vector3.zero; //The current eulerAngles

function Start() { eulers = transform.eulerAngles; }

function Update() { //Rotate eulers.x += Input.GetAxis("Vertical") pitchSpeed Time.deltaTime; eulers.x = Mathf.Clamp(eulers.x, minPitch, maxPitch); eulers.z += Input.GetAxis("Horizontal") rollSpeed Time.deltaTime; eulers.z = Mathf.Clamp(eulers.z, minRoll, maxRoll); //eulers.y += Input.GetAxis("Lateral") rollSpeed Time.deltaTime; transform.eulerAngles = eulers;

     transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);

transform.Rotate ((-Vector3.forward) Input.GetAxis("Horizontal") rollSpeed * Time.deltaTime);

//doesn't work below: //the idea is to turn left and right (Vector3.up) by a factor of the tilt left and right rotation variable (eulers.z) given controls (i.e. ...GetAxis("Horizontal") and speed (i.e. yawSpeed) transform.Rotate (Vector3.up*Input.GetAxis("Horizontal") eulers.z yawSpeed * Time.deltaTime); // this was an attempt to get left and right turning-- it jerks around and conflicts with previous line

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Manually computing rotation constrain limits 0 Answers

c# problem with limiting rotation (with rigidbody) 1 Answer

Lock rotation (range of motion) of auto-aiming bullet spawner 2 Answers

Limiting rotations to box edge 1 Answer

Trying to use Gyro to see the scene in unity but can't limit the camera 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges