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 iron_attorney · Jul 03, 2015 at 04:47 PM · c#rotationvector3aicross

Spacesim AI pilot, vector3.cross issues

Hi there, I am trying to get some space sim AI piloting off the ground. I am using vector3.cross to help determine which way the AI pilot needs to turn to face a target, and have ran into problems. Here is the code:

 void TurnToTarget () {
 
         Vector3 neededRotation = aiEngine.destination - transform.position;
 
         if (Vector3.Angle(transform.forward, neededRotation) > 1) {
 
             Debug.Log (Vector3.Angle(transform.forward, neededRotation));
 
             facingTarget = false;
 
             Vector3 cross = Vector3.Cross (transform.forward, neededRotation);
 
             Debug.Log (cross);
 
             Roll ();
 
             if (cross.x != 0) {
 
                 Pitch (cross.x);
             }
 
             if (cross.y != 0) {
                 
                 Yaw (cross.y);
             }
 
         } else {
 
             facingTarget = true;
         }
     }

Roll () doesn't do anything yet, but pitch and yaw take the x and y components of vector3.cross respectively, and use the sign of the x or y to decide which direction to turn in. This nearly works, except the AI sometimes gets confused and flies directly away from the target instead of directly towards it. I thought I could fix this by throwing in the if (Vector3.Angle(transform.forward, neededRotation) > 1) buisness, but it doesn't totally help.

I'm suspecting the reason for this is that when the ship's rotation changes, the position of the positive and negative .cross componenets can change meaning the ship might try to turn left instead of right, or up instead of down. I don't yet know if this is correct, but if it is, what can I do to stop this? (Aside from keep my ship perfectly level at all times!). Is there a way to make the vectors more localised? Or is there a quaternion equivilent that might work better?

Cheers in advance for your help,

Pete

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nanity · Jul 03, 2015 at 05:28 PM

Quaternions all the way. Had very bad experiences with Vector3 myself.

 Vector3 neededRotation = aiEngine.destination - transform.position;
 Quaternion neededRotationQ = Quaternion.LookRotation(neededRotation);
 transform.rotation = neededRotationQ;

Imo this looks a bit edgy with no interpolation, so you might use:

 //instead of transform.rotation = neededRotationQ;
 float interpolationSpeed = 10.0f;
 transform.rotation = Quaternion.Slerp(transform.rotation, neededRotationQ, Time.deltaTime * interpolationSpeed);

Best tip for quaternion rotations for your future:

 A and B are quaternions:
 A * B = Applying local rotation B on A = Applying global rotation A on B
Comment
Add comment · Show 2 · 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 iron_attorney · Jul 03, 2015 at 07:18 PM 0
Share

Ok, lookRotation sounds promising. Although I should point out at this point that I don't want to rotate the AI ship with this script, just let it know which way it wants to rotate so it can feed that info to it's "flight computer". The informationw also wants to be split up into yaw-axis ratotion and pitch-axis rotation so the AI can specifically decide to yaw or pitch (and eventually roll). I've not used lookRotation in this context before, but I'll investigate, see what I can get working.

Cheers for your help!

Pete

avatar image Nanity · Jul 03, 2015 at 10:13 PM 0
Share

Still don't use Vector3, in your own interest. This means don't use yaw pitch and roll.

If you are in outer space, it doesn't matter if you look at a point from top or bottom. It will look the same.

If you are in an orbit of a planet where you are influenced by gravity you gonna have a feeling for up and down (rolling). But this totally alters your gameplay, camera view and code.

Is the camera fixed relative to the "world origin" or to the pilot regarding the rotation (not position!)? I mean does the camera flip if the space ship flips?

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

slowly rotate a object *need quick fix* 0 Answers

How to work out dogfight rotations for AI 0 Answers

Rotate a vector based on mouse x coordinates? 1 Answer

transform.Rotate is rotating all axes, why? 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