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
0
Question by skail · Jun 06, 2011 at 04:01 PM · javascript

How to calculate the point of intercept in 3D space

So I have been wracking my brain trying to figure this out. So I need to calculate how far ahead to aim for 1 spaceship to fire and hit another spaceship. I need to know this so when the player targets another ship a targeting bracket shows up where the player needs to fire at that point in time. I know there is a code already written to do this on the wiki, however I am not understanding how it works. I am scripting in JS. Any help with this would be greatly appreciated.

Comment
Add comment · Show 2
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 flaviusxvii · Jun 06, 2011 at 04:17 PM 0
Share

Can you paste a link to the script in the wiki?

avatar image skail · Jun 06, 2011 at 04:26 PM 0
Share

Oh sorry: http://www.unifycommunity.com/wiki/index.php?title=Calculating_$$anonymous$$_For_Projectiles

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jun 06, 2011 at 05:06 PM

Ok, i don't see your problem... Do you want to understand how it works or how you can use it?

Just copy the code in this section:
http://www.unifycommunity.com/wiki/index.php?title=Calculating_Lead_For_Projectiles#Calculating_the_intercept_point
into a new C-sharp script that is located in Standard Assets. Then you can use the function even in JS.

 using UnityEngine;
 using System.Collections;
 
 public class Intercept
 {
 
     //first-order intercept using absolute target position
     
     public static Vector3 FirstOrderIntercept(  Vector3 shooterPosition,
                                                 Vector3 shooterVelocity,
                                                 float shotSpeed,
                                                 Vector3 targetPosition,
                                                 Vector3 targetVelocity) {
         Vector3 targetRelativeVelocity = targetVelocity - shooterVelocity;
         float t = FirstOrderInterceptTime(  shotSpeed,
                                             targetPosition - shooterPosition,
                                             targetRelativeVelocity);
         return targetPosition + t*(targetRelativeVelocity);
     }
     //first-order intercept using relative target position
     public static float FirstOrderInterceptTime(float shotSpeed,
                                                 Vector3 targetRelativePosition,
                                                 Vector3 targetRelativeVelocity) {
         float velocitySquared = targetRelativeVelocity.sqrMagnitude;
         if(velocitySquared < 0.001f)
             return 0f;
     
         float a = velocitySquared - shotSpeed*shotSpeed;
     
         //handle similar velocities
         if (Mathf.Abs(a) < 0.001f) {
             float t = -targetRelativePosition.sqrMagnitude
                         /(2f*Vector3.Dot(   targetRelativeVelocity,
                                             targetRelativePosition));
             return Mathf.Max(t, 0f); //don't shoot back in time
         }
     
         float   b = 2f*Vector3.Dot(targetRelativeVelocity, targetRelativePosition),
                 c = targetRelativePosition.sqrMagnitude,
                 determinant = b*b - 4f*a*c;
        
         if (determinant > 0f) { //determinant > 0; two intercept paths (most common)
             float   t1 = (-b + Mathf.Sqrt(determinant))/(2f*a),
                     t2 = (-b - Mathf.Sqrt(determinant))/(2f*a);
             if (t1 > 0f) {
                 if (t2 > 0f)
                     return Mathf.Min(t1, t2); //both are positive
                 else
                     return t1; //only t1 is positive
             } else
                 return Mathf.Max(t2, 0f); //don't shoot back in time
         } else if (determinant < 0f) //determinant < 0; no intercept path
             return 0f;
         else //determinant = 0; one intercept path, pretty much never happens
             return Mathf.Max(-b/(2f*a), 0f); //don't shoot back in time
     }
 }


In JS you can use it like this:

 // I'll assume that both target and shooter are rigidbodies
 var target : Transform;
 var shotSpeed : float;
 
 function Update()
 {
     var interceptionPoint = Intercept.FirstOrderIntercept(transform.position, rigidbody.velocity, shotSpeed, target.position, target.rigidbody.velocity);
     // Do what you want
 }
Comment
Add comment · Show 6 · 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 skail · Jun 06, 2011 at 05:45 PM 0
Share

I am trying to understand how the math and the code works. I have copied and pasted the code and can get it to work, but it seems to be off somewhere positioning my targeting bracket off by enough to cause problems, so I am trying to understand the code to figure out what is wrong.

avatar image flaviusxvii · Jun 06, 2011 at 06:43 PM 0
Share

Bleh.. there's some pretty sexy linear algebra going on. Are you familiar with the discipline?

avatar image skail · Jun 06, 2011 at 07:08 PM 0
Share

I'm relatively familiar with linear algebra and usually am fairly good with math but for some reason i'm not able to look at this code and see it in simple y=mx+b terms here. I am fairly new to unity so I think it might be that the javascript/unity speak is tripping me up with the equations here.

avatar image Bunny83 · Jun 06, 2011 at 07:23 PM 1
Share

Well, i don't try to explain the math :D but it works great. I think your problem is the shooterVelocity. If your projectile uses a fix world-speed you should pass Vector3.zero as shooterVelocity.

This code assumes that the shotSpeed gets added to your own speed(shooterVelocity).

avatar image skail · Jun 06, 2011 at 07:34 PM 0
Share

ah, that might be where the problem was. I would still like to better understand the math so I can fully understand the code im using but I think that might be the issue because the shots were either just before or behind the lead bracket.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Swap texture2D on enemy sprite once reached a certain point 1 Answer

Recursive Raycast Spatial Search 0 Answers

Find Keyword in a Searched GameObject (JS) 2 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