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 sevensixtytwo · Sep 15, 2014 at 09:25 AM · rotationvelocitytanksballistic

Deriving a velocity from a camera movement

or "Can I Interpret Angular Movement as Linear Velocity" or "How I learned to stop worrying and love Mathematics".

Hi again, Unity Answers fellas.

I'm currently trying to simulate a modern battle tank's Fire Control System in Unity. Here's a summary of what I have so far:

  1. The sight is pointed at a target (aimpoint)

  2. A laser (raycast) is shot out to get the range

  3. The aimpoint is adjusted upwards depending on distance, muzzle velocity, and tank tilt

  4. Lead is added depending on the tank's current velocity as well as the target's

  5. The turret slews around and elevates the gun to center on the final aimpoint

  6. The shot is fired and hits the target. On the way!

As it stands, it's already pretty good. Hitting a moving target (while moving yourself) at less than a 1000m is a cakewalk while doing the same thing at 3000+ meters might make you sweat a little bit. But I want to make it better. Turn your attention to number 4.

"Lead is added depending on the tank's current velocity as well as the target's"

This literally means I get the velocity of the rigidbody attached to the target and do my calculations from there. It works fine and dandy if the Gunner is an NPC who can store a reference to the bad guy in his scripts somewhere and just take a peek at his velocity now and then. But what if the Gunner is the player?

alt text

Here's a shot of my totally-WIP Gunner's Primary Sight Extension. The red numbers below the crosshairs are the range to the target. The light gray cross is the mouse cursor.

The GPSE, as a turret, isn't controlled using the traditional Mouse Look. Instead, the distance between the cursor and the center of the screen is calculated and the view rotates accordingly. If any of you have played M1 Tank Platoon 2 (I have) or Steel Beasts (I haven't), you'd know that the lead is actually calculated from the rotation speed of the viewport and the distance to the target. Hence, once you are able to keep a moving target in the center of the screen, the turret will automatically add lead, ensuring a hit (unless the target suddenly changes direction while the shell is in flight).

So here's what I want to know:

"Can I conjure up a velocity value based on the movement of the center of the screen and taking into account the distance and magnification?"

or

"If I aim my camera at an imaginary point a specific distance away from me and pan the camera slowly, can I get that moving imaginary point's velocity?"

leadingthetarget.png (294.7 kB)
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
Best Answer

Answer by sevensixtytwo · Sep 15, 2014 at 03:42 PM

Whelp, here I go again. I'll just leave this here in case another person tries to implement a lead-assist for ballistic projectiles. Note that you'll need to use it with this script on the wiki.

Calculate Lead For Projectiles

Some of us might know this dandy equation:

V=wr

where V = Linear Velocity, w = Angular Velocity and r = radius

Yay for astronomy! So then it becomes a matter of finding the r and the w. The r was pretty easy. The hitpoint.distance of the raycast would serve. w on the other hand, was a bit trickier...

I had to overhaul my mouse tracking code for the turrets, making the rotation speed dependent on the current field of view (magnification).

     //our target's "velocity", moveX and moveY are the speeds the camera is rotating at
     //minor confusion, the moveX is actually the camera's Y-rotation and vice-versa.
     //for Z, see later.
     var trackingVelocity = Vector3(moveX,-moveY,Z);

Before we can use it, it has to be converted to world space since it's currently relative to the camera.

     trackingVelocity = viewport.transform.TransformDirection(trackingVelocity);

And then finally multiply trackingVelocity (w) by the distance (r) to get the final linear velocity (V). Now it can be used as the targetVelocity component of my CalculateLead function.

      var targetVelocity = trackingVelocity * hit.distance;
      finalAimpoint = FirstOrderIntercept(shooterPosition,shooterVelocity,shotSpeed,targetPosition,targetVelocity);

Works like a beast, though I am still working on the Z component of the velocity. I'm trying storing the hit.distance of every frame in LateUpdate and comparing it to the new one in Update. More updates soon. Unless anyone else pitches in, I'll mark this as answered once I finish it.

UDATE: Mission accomplished. I ended up saving the hit.point at the end of every frame and then using:

       Z = (previousHit - newHit).magnitude * Time.deltaTime;
       targetVelocity.z = Z;

The end result is much more accurate. Targets moving 40MPH at 1000+ meters can be hit consistently but it still requires a steady hand since any jerk or twitch can send the shot flying wildly off target. Perfect.

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

23 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

Related Questions

Instantly rotating and changing an object's velocity relative to another (probably quaternion mistake!) 1 Answer

Unity C# 2D Adding Velocity on rotation 1 Answer

Gun not firing in certain direction 1 Answer

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

How do you tweak an object's velocity based on it's direction? 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