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
1
Question by mhughson · Sep 10, 2012 at 03:10 AM · rotationmovementquaternionmathflying

Flight Movement with Banking

I am attempting to make an airplane automatically fly around a series of points in space. Right now the movement is locked to a plane along the X/Z axis (Y never changes) but ideally I would like to keep the option for full 3D flight open. The airplane constantly moves forward, while rotating around the Y axis to face its target, causing a little bit of an arc as it moves towards its target.

So far it is working well, but I am not sure how to add banking to the rotation calculations. As the plane makes sharp turns I want the plane to rotate around its forward axis.

I am just using the built in Quaternion.LookRotation function. I've tried setting the Z-rotation directly after the rotation towards the target is complete, but it seems to cause problems with the movement towards the target, and sends the plan off in undesired directions.

     // Update is called once per frame
     void Update () 
     {
         // Get the current target.
         Transform currentTarget = targets[currentTargetIndex];
         
         // Get a vector from the current position to the target.
         // This is the vector we want to move our forward vector towards.
         Vector3 lookDirection = currentTarget.position - transform.position;
 
         // Calculate the rotation needed for this target look vector.
         Quaternion rot = Quaternion.LookRotation(lookDirection.normalized);
 
         // Rotate towards that target rotation based on the turn speed.
         transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * turnSpeed);

         // With the rotation complete, just move forward.
         transform.Translate(new Vector3(0,0,moveSpeed) * Time.deltaTime);
     }

Let me know if there is anymore information I can add.

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
3

Answer by ThermalFusion · Sep 10, 2012 at 07:34 AM

Try:

 float bank = 30f;
 Quaternion rot = Quaternion.LookRotation(lookDirection.normalized) * Quaternion.AngleAxis(bank, Vector3.forward);
Comment
Add comment · Show 5 · 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 mhughson · Sep 12, 2012 at 04:05 AM 0
Share

This seems to have the same issues as my original attempts. The rotation around the forward vector is not kept separate from the rotation around the up. The result is that the next frame Quaternion.LookRotation tries to remove the forward-axis rotation we added in the previous frame. The result is some awkward loop-d-loops. At least I think that's the cause.

avatar image Muuskii · Sep 12, 2012 at 04:46 AM 0
Share

Don't forget to mark an answer as correct so that other people know that the question is resolved. :)

avatar image ThermalFusion · Sep 12, 2012 at 07:04 PM 0
Share

You are right, this does introduce some movement in the y axis aswell. This is probably because the shortest rotation towards the target rotation is not around the y axis if you introduce some banking. There might be some magic to get this working in the xz plane, but it is beyond me. Here's another go I had on it:

 void Update () 
 {
     Transform currentTarget = targets[currentTargetIndex];
     Vector3 lookDirection = currentTarget.position - transform.position;
 Vector3 normalizedLookDirection = lookDirection.normalized;
 Debug.DrawRay(transform.position, lookDirection, Color.blue);
 Debug.DrawRay(transform.position, transform.right, new Color(0.5f - (Vector3.Dot(transform.right, normalizedLookDirection)*0.5f), 0.5f ); (Vector3.Dot(transform.right, normalizedLookDirection)*0.5f), 0f));
 float bank = maxBank * -Vector3.Dot(transform.right, normalizedLookDirection);
 Quaternion rot = Quaternion.LookRotation(normalizedLookDirection) * Quaternion.AngleAxis(bank, Vector3.forward);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime * turnSpeed) ;
     transform.Translate(new Vector3(0,0,moveSpeed) * Time.deltaTime);
 }

But as stated, this does make the object start moving in the y axis aswell.

avatar image ThermalFusion · Sep 12, 2012 at 07:16 PM 1
Share

So I had another go at it, this time using a sub object to apply the banking to, it's a bit of a hack but worked great for me. You will need a child below the plane that you apply the banking to, Transform banker in the example:

 public Transform[] targets;
 public int currentTargetIndex = 0;
 public float turnSpeed = 10f;
 public float maxBank = 30f;
 public float moveSpeed = 2f;
 public Transform banker; // Child of this transform
 void Update () 
 {
     Transform currentTarget = targets[currentTargetIndex];
     Vector3 lookDirection = currentTarget.position - transform.position;
 Vector3 normalizedLookDirection = lookDirection.normalized;
 float bank = maxBank * -Vector3.Dot(transform.right, normalizedLookDirection);
 Quaternion rot = Quaternion.LookRotation(normalizedLookDirection);
 banker.localRotation = Quaternion.AngleAxis(bank, Vector3.forward);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime * turnSpeed);
     transform.Translate(new Vector3(0,0,moveSpeed) * Time.deltaTime);
 }

This should keep the object on the xz plane while having banking aswell.

(Had to edit a lot to make the formatting work properly)

avatar image mhughson · Sep 13, 2012 at 05:11 AM 0
Share

Great idea using parenting to isolate the rotation. That worked great thanks!

I've added a full class implementation to the thread but am just waiting for mod approval.

avatar image
1

Answer by sandeepsmartest · Nov 10, 2012 at 02:23 PM

hey its really great bro.i too have the same issue i.e., me flight is turning in a curved way bt its not banking during turnings.actually i achieved banking and curved turning of the flight separately but i dont know how to integrate it together,below code makes the flight to bank: float fullbank = 40; float h = input.getaxis('horzontal") plane1.transform.rotation = Quaternion.Slerp(plane1.transform.rotation,Quaternion.AngleAxis(fullBank,Vector3.forward -h),Time.deltaTime 1.5f); but i want to bank my flight during turnings.plz help me .

Comment
Add comment · Show 1 · 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 AlucardJay · Nov 10, 2012 at 02:23 PM 0
Share

Hi There.

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Simulated Sword Swing - Sword Floating Away 0 Answers

Directional booster 1 Answer

quaternion - rotate camera based on original facing at scene start? 0 Answers

Follow rotation on Y axis ONLY clockwise/counterclockwise. 1 Answer

Rotation reversing for a single frame while turning. 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