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 /
  • Help Room /
avatar image
0
Question by foxdie01 · Oct 20, 2016 at 08:35 AM · rotationmathsturretsballistic

Better Barrel and Turret movement with ballistic targeting + drag [c#]

Hi I'm new to unity, and C# in general. But I've made some decent progress but i'm a long way off from totally understanding everything i'm stitching together from various tutorials (Quaternion calculations and radons etc just completely allude me).

Basically i have tank, the turret need to rotate towards the target on the Y axis, and the gun barrel then needs to elevate to the target + the extra needed for the trajectory. I've actually done this and works fine. [pic0]

My issues:

  • Some tanks need to have limited turret rotation &| barrel elevation ( eg tank destroyers vs turreted tanks).

  • I have two workings versions of the turret rotation script, the first is commented out. Now the first is actually better, because it locks the turret to the parent axis and prevents tilting or elevating, which is what i actually want - but I just cant seem to incorporate this into a workable autoaim [pic1]. Whereas version two (not commented out works, but is cheating by keeping the turret horizontal to the world at all times [pic0]

  • oh and finally, as a bonus i'd love someone to tell me how i can incorporate drag into my ballistic calculator so i'll have a variable i can divide the penetration value to for shots a long range. Ideally i'd like to set "2" to the drag field in the projectiles rigidbody and then incorporate "2" into the ballistic calculator. But i fear this going to require some advance mathematics based on distance, and while i understand (barely) the wiki page on the subject, im not sure how i can combine both calculations into a single formula.

So here's my code:

     void Update () {
 
         if (target != null) {
             /*
             Quaternion qTurret;
             Transform trans = turret;
             float distanceToPlane = Vector3.Dot(trans.up, target.transform.position - trans.position);
             Vector3 planePoint = target.transform.position - trans.up * distanceToPlane;
 
             qTurret = Quaternion.LookRotation(planePoint - trans.position, trans.up);
             trans.rotation = Quaternion.RotateTowards(turret.transform.rotation, qTurret, (moveSpeed * 10) * Time.deltaTime);
             Vector3 OurPos = this.gameObject.transform.position;
             float angle1 = scripts.GetComponent<BallisticCalc> ().CalculateTrajAngles (shellVelocity, OurPos, target.transform);
             barrel.transform.localEulerAngles = new Vector3 (angle1, 0, 0);
 
             */
 
             Vector3 OurPos = this.gameObject.transform.position;
             float angle1 = scripts.GetComponent<BallisticCalc> ().CalculateTrajAngles (shellVelocity, OurPos, target.transform);
 
 
             Vector3 destDir = target.transform.position - turret.position;
             destDir.y = 0;
 
             Quaternion targetRotation = Quaternion.LookRotation (destDir);
             turret.rotation = Quaternion.RotateTowards (turret.rotation, targetRotation, (moveSpeed * 10) * Time.deltaTime);
 
 
             barrel.transform.localEulerAngles = new Vector3 (angle1, 0, 0);
 
 
 
         }
     
     }

and my ballistic calculator:

     public float CalculateTrajAngles(float velocity, Vector3 start, Transform target){
 
 
         float v = velocity;
         Vector3 targetVector = target.position - start;
         float height = targetVector.y;
         targetVector.y = 0;
 
         float x = targetVector.magnitude;
         float y = height;
         float g = Mathf.Abs(Physics.gravity.y);
 
         float top = v*v + Mathf.Sqrt(v*v*v*v - (g*(g*x*x + 2*y*v*v)));
         float bottom = g*x;
         float angle1 = Mathf.Atan2(bottom,top);
 
         return -angle1 * Mathf.Rad2Deg;
 
 
     }

Any help appreciated.

pic0 pic1

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 foxdie01 · Oct 20, 2016 at 03:55 PM

So not a graceful but one fix to issue 2 is:

  1. solution one is visibly accurate but not ballistically accurate.

  2. Solution two is ballistically accurate but not visibly accurate.

So my visible turret and barrel rotates using the first solution. And i have some empty gamobjects simulating solution 2 which is where my "exit" spawns the projectile.

Like i say not graceful. but solves the issue, sure the spawn point doesn't match the end of the visible barrel but nothing that wont be masked by plumes of smoke.

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

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

Turret rotation based on its own local rotation. 3 Answers

Unit vector (rot) between parent and child 0 Answers

How to fix turret rotation offest by 90 degrees 2 Answers

Applying a rotation relative to the object's starting rotation 0 Answers

Calculating the Hit Point on a Sphere (with Rotation, Based on Static Nodes) 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