Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 The_r0nin · Jun 04, 2011 at 05:16 PM · rotationaircraft

Trouble orienting a GO using transform.up

I am coding the AI for my airplane-like enemies. Due to their nature as aircraft, they have a constant forward speed and would change heading by rotating their up vector in the direction of the enemy, then pitching up until the nose is pointing at the enemy. I did this using the game logic that follows:

First, I found the target position with respect to the plane (InverseTransformPoint), then I ignored the z component to collapse the problem to a two dimensional one. Then I used Atan2 to get the rotation angle from the forward vector of the plane to the target. Finally, I send it to the aircraft script and LERPed the angle. The aircraft "rolls" in its FixedUpdate. The script looks like:

In the AI choice switch statement:

 case 3:
     objective = AIObjective.attack;
     primaryTarget = SelectNearbyAllies();
     desiredPitch = -.5;
     if (!primaryTarget) {
         objective = AIObjective.straight;
         desiredPitch = 0;
     }
 break;

Later, set the target as the enemy GO:

 case AIObjective.attack:
     targetPoint = primaryTarget.transform.position;
 break;

Then find the proper angle:

 var tempVec: Vector3= transform.InverseTransformPoint(targetPoint);
 desiredRoll = (Mathf.Atan2(tempVec.y,tempVec.x) * Mathf.Rad2Deg)+90;
 if (desiredRoll < 0) desiredRoll +=360;
 if (tempVec.x*tempVec.x + tempVec.y*tempVec.y < .001) desiredRoll = 0;

 SendRoll(desiredRoll);
 SendPitch(desiredPitch);

And in the aircraft script:

 var newRoll = Mathf.MoveTowardsAngle(0, -roll - transform.eulerAngles.z, rollSpeed*Time.deltaTime);
 if (newRoll != 0) transform.RotateAround(transform.position,transform.forward, newRoll);

 if (pitch != 0) transform.RotateAround(transform.position,transform.right,pitch*Time.deltaTime);


Any advice would be appreciated. Please remember that I must send only the amount to be rolled and the necessary pitch to the aircraft script (without a serious rewrite), and that the aircraft can only pitch from transform.forward towards transform up (along that 2D plane). Thanks!

EDIT:

I've gotten close with the following:

 if (!primaryTarget) return;
 var tempVec: Vector3 = primaryTarget.transform.position - transform.position;
 tempVec = tempVec.normalized;
 var upVec: Vector3 = Vector3.Cross(transform.forward, tempVec);
 upVec = upVec.normalized;
 var combinedVec: Vector3 = Vector3.Cross(tempVec,upVec);
 combinedVec = combinedVec.normalized;
 Debug.DrawLine(transform.position, transform.position + tempVec *10, Color.blue);
 Debug.DrawLine(transform.position, transform.position + upVec *10, Color.red);    
 Debug.DrawLine(transform.position, transform.position + combinedVec *10, Color.yellow);

 var dR: float = Mathf.Atan2(combinedVec.z,combinedVec.y) * Mathf.Rad2Deg;
 if (dR < 0) dR +=360;
 dR = 360-dR;
 Debug.Log(dR);

 SendRoll(dR);

Now my problem is that I can't pitch towards the target... once I do the plane spins erratically or not at all... Once again, help would be appreciated!

Comment
Add comment · Show 1
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 The_r0nin · Jun 04, 2011 at 09:06 PM 0
Share

Above I said that I needed help with just the roll amount per frame... but if anyone has a better way to reorient the plane along the transform.up and then pitch... I'm more than open to it!

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Aircraft altimeter 2 Answers

Need help converting Euler-based rotation to Quaternion-based (gimbal lock). 1 Answer

Preventing rotation of rigidbody at a specific rotation 0 Answers

Aircraft Control by Animations ? 0 Answers

Rotate an object back to its old rotation 1 Answer


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