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 Saif_youssef89 · May 11, 2019 at 11:42 PM · raycastpositionquaterniontrajectorypredict

Predicting hit coordinates of a moving object

I've found myself experimenting with a challenging idea, and would appreciate some help.


Lets say I have a small pirate game, 2 ships have cannons and are moving. I'm trying to have the ships determine (predict) the coordinates to which they fire their cannons so that they collide with the other moving ship.


I cannot use the usual Raycast as it will shoot at the CURRENT coordinates of the enemy ship, not the predicted one.

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 sjhalayka · May 12, 2019 at 12:49 AM 0
Share

You can cheat and use the current position plus current velocity of the ship B to predict its path x seconds in the future.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Cornelis-de-Jager · May 12, 2019 at 09:49 PM

I have shamelessly stolen this code from @peterho0218 on this thread: Click Me

 private Vector3 predictedPosition(Vector3 targetPosition, Vector3 shooterPosition, Vector3 targetVelocity, float projectileSpeed)
 {
     Vector3 displacement = targetPosition - shooterPosition;
     float targetMoveAngle = Vector3.Angle(-displacement, targetVelocity) * Mathf.Deg2Rad;
          
     //if the target is stopping or if it is impossible for the projectile to catch up with the target (Sine Formula)
     if (targetVelocity.magnitude == 0 || targetVelocity.magnitude > projectileSpeed && Mathf.Sin(targetMoveAngle) / projectileSpeed > Mathf.Cos(targetMoveAngle) / targetVelocity.magnitude)
     {
         Debug.Log("Position prediction is not feasible.");
         return targetPosition;
     }
          
     //also Sine Formula
     float shootAngle = Mathf.Asin(Mathf.Sin(targetMoveAngle) * targetVelocity.magnitude / projectileSpeed);
     return targetPosition + targetVelocity * displacement.magnitude / Mathf.Sin(Mathf.PI - targetMoveAngle - shootAngle) * Mathf.Sin(shootAngle) / targetVelocity.magnitude;
 }


Comment
Add comment · Show 1 · 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 Saif_youssef89 · May 18, 2019 at 05:38 AM 0
Share

Thanks for you help @Cornelis-de-Jager, works as intended!


Had to change the code a bit since one of the objects didnt have a Rigibdoy and was using Translate for movement, which doesn't work with physics.

avatar image
0

Answer by wyatts · May 12, 2019 at 01:13 AM

Just spitballing here: I'd start with the variables,

  • the distance between boats

  • target boat velocity

  • cannon travel time

You can then use those variables to create a box collider that leads the target boat with an offset in the direction of movement that adjusts its offset based on the target boats velocity, and the distance between the target boat and the firing boat.

Then you can have the canons raycast out and when they hit that collider and fire, their cannonballs should hit the ship if its speed / direction / distance hasn't changed that much (and you've done your math right!).

But I'm sure there are a billion ways to do it

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 Saif_youssef89 · May 12, 2019 at 07:15 PM 1
Share

Interesting, i think that would probably give accurate results. The math required is a bit above my level, and i still havent had a full grasp of Quaternions. I'll give it a shot and post what i could come up with. Thanks for the idea @wyatts .

If you have any other ideas, please feel free to share :)

avatar image wyatts Saif_youssef89 · May 12, 2019 at 07:31 PM 0
Share

Another option is you could skip the box collider/raycast business and just measure your canons rotation from the target to the predicted-target (Vector3.Angle). You'd use the same variables just have it affect the rotation of the canon.

The math for either method shouldn't be too bad -- if you think about it you're just drawing a triangle between 3 points, the canon, the target ship, and the predicted target.

Also I'd note you don't need a deep understanding of quaternions for this. Basic functions like Quaternion.RotateTowards or Quaternion.LookRotation allow you to point your objects to vector3s without having to manually rotate angles. Good luck!

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

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

Find trajectory angle needed to hit an object 0 Answers

Changing position of a RayCast 1 Answer

Instantiating Object On Map 1 Answer

Move child to position and bring parent 1 Answer

Dynamically Change Position of RayCast 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