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
2
Question by Wopsie · Jan 12, 2016 at 09:48 AM · turrettargeting

How do i predict the position of my player for the turret to shoot at?

I have a turret and a player spaceship. this turret shoots at the player within a certain range but, as the player fliest around, every shot misses. can any of you possibly help me establish a mathmatical formula to calculate this?

what values do i need to put where etc. any help is greatly appriciated, i have been breaking my head over this for the last few days

Comment
Add comment · Show 3
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 Munchy2007 · Jan 12, 2016 at 12:10 PM 0
Share

This code from danikgames.com works perfectly

http://danikgames.com/blog/moving-target-intercept-in-3d/

avatar image devilsid · Jan 13, 2016 at 02:18 PM 0
Share

you can just destroy/disable the turret after some time as per your needs. you will need to adjust the turret speed and time for this. calculating distance every frame will not be good for performance

avatar image · Aug 07, 2017 at 10:38 AM 0
Share

Hey, sorry, for that but i'm reaaaly new at unity, and at the coding at all!Could you give me the scripts you've used?

Regards, Nikola

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by peterho0218 · Nov 22, 2016 at 06:42 PM

We can simply solve this by using Sine Formula. alt text For the method in Unity:

 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;
     }

You may call it in Update() method to use it.


picture.png (14.7 kB)
Comment
Add comment · Show 3 · 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 landon912 · Nov 22, 2016 at 06:49 PM 0
Share

I haven't looked past your velocity > projectile because that line is wrong. Your target can have a larger magnitude than your projectile's and still intersect. For example, a target moving closer to the projectile's origin. The projectile can still intercept the target. You should check the dot product first.

avatar image peterho0218 landon912 · Nov 24, 2016 at 09:10 PM 0
Share

Thank you for your re$$anonymous$$der. I have improved my code now.

avatar image Wopsie · Nov 23, 2016 at 12:44 PM 0
Share

Funny to see responses to this old thread. I actually got it working back when i asked this. The code looked something like this:

 float targetDistance = Vector3.Distance(shotPos, targetPos);
 float traveltime = targetDistance / bulletSpeed;
 Vector3 predictedPos = targetPos + target$$anonymous$$ovement * traveltime;
 return predictedPos;

This worked alright at the time, it wasnt perfect by any means but it made me not needing to deliberately program a margin of error for the turret.

This example you gave here seems much more reliable, thanks for this whole equation!

avatar image
0

Answer by Wopsie · Jan 13, 2016 at 10:33 AM

i finally got it right only a little while before your comment! i ended up doing this: (i happened to know the speed of my bullet never changes)

 //get player position
             Vector3 playerPos = turretRange[0].transform.position;
             //get bullet speed
             float bulletSpeed = 600f * Time.fixedDeltaTime;
             //calculate distance to player with pythagoras
             float playerDistance = Vector3.Distance(transform.position, playerPos);
             //calculate traveltime
             travelTime = playerDistance / bulletSpeed;
             predictedPosition = playerPos + playerData.movementVector * travelTime;

thanks a lot anyway!

Comment
Add comment · Show 3 · 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 Bonfire-Boy · Jan 13, 2016 at 10:54 AM 0
Share

That's not going to be quite right, because you calculate the travelTime based on the current player position. So the bullet is going to arrive at the current player position at the same time the player arrives at the predicted position.

avatar image Wopsie Bonfire-Boy · Jan 14, 2016 at 09:57 AM 0
Share

well my playerdata.movementVector is the diffrence between where the player was before and after he changed position, i add that to the playerpos and multiply it by traveltime, it gives me the desired effect

avatar image chintan_shroff · May 10, 2016 at 06:19 PM 0
Share

Hi Wopsie, can you kindly tell me what is playerData.movementVector do??

predictedPosition = playerPos + playerData.movementVector * travelTime;

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

11 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

Related Questions

Locking on target with angle (mostly working) 1 Answer

My turret is a pacifist. 2 Answers

Tower Defense turret with multiple targets within range. Help with targeting and nulling. 1 Answer

MoveToward and LookForward problem 1 Answer

Object rotate to target in 2D? Not working properly. 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