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 fredinger · Mar 27, 2016 at 01:24 AM · physicstower-defensetower defense

Tower Defense: Calculate Force of projectile depending on Enemy position

Hi everybody,

I want to make a Tower Defense game and I was questioning how to calculate the force of the projectile depending on the position of an Enemy. I made a little sketch to show how I wanted to shoot the projectile. I want to fire the projectile without any tilt, because otherwise the arrow might go through the tower and if the arrow is shot without any gravity it will look unrealistic. alt text

Is there some sort of mathematical formula or does Unity provide any Method to calculate this? If someone can help I'll be very thankful. Sorry for my bad englisch.

towerdefense.png (13.0 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eno-Khaon · Mar 27, 2016 at 03:29 AM

I'm going to cheapskate this a little bit and refer you to a previous answer I've given. Your diagram and post don't explicitly state whether you're referring to a 2D or 3D game, so I'll simply offer two relevant links:

Here is my response to a question regarding 2D trajectory: http://answers.unity3d.com/questions/1124197/how-can-i-launch-a-gameobject-at-a-target-if-i-am.html

That also links to my main post relating to 3D trajectory: http://answers.unity3d.com/questions/1087568/3d-trajectory-prediction.html#answer-1087707

By your description, you would be able to base it on my second (3D) example of:

A -> B, attempting to arrive based on a specified launch angle

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 fredinger · Apr 21, 2016 at 11:41 AM 0
Share

Thanks for all the answers. Eno $$anonymous$$haon anwer worked. The reason I needed so long is that I had a lot of stress in school and I wanted to find out, how Eno $$anonymous$$haon came to this formula. So I tried to recreate to formula with the Free Fall and the Throw. In case anyone wants to know how I calculated it, I'll attach some scans. The problem with my formula is, that it isn't the same, maybe someone knows, where my calculations are wrong. In case anyone is wondering, it is written German.

So again, thanks for all the anwers.

alt text

alt text

scan0001.jpg (215.0 kB)
scan0002.jpg (62.0 kB)
avatar image
1

Answer by fideltfg · Mar 27, 2016 at 01:59 PM

I use this, found it myself a while back, but works for most cases where you need to hit a moving target from a static one.

Rather then calculating the speed the round needs to go it calculates a new aim point given the targets speed, distance and the known speed the round will travel. if you adjust the speed of the round then it will be more unrealistic then not using gravity. You will have to test a bit to get the correct speed for your rounds, to simulate the effects you want, but then run through his and you should get what your after.

It does add a 'tilt' to the projectile, but you can adjust the spawn point to avoid it spawning within the weapon itself. And of cause this only works to launch the round at the target. if the target seeds up or changes direction the round will miss.

use it like this

  void Update(){
      transform.LookAt(CalculateLead(target, weapon.transform,  speed))                 
      transform.forward = GetComponent<Rigidbody>().velocity;
     } 
 
     
     
     public static Vector3 CalculateLead(GameObject target, Transform t, float speed)
         {
             Vector3 V = target.GetComponent<Rigidbody>().velocity;
             Vector3 D = target.transform.position - t.position;
             float A = V.sqrMagnitude - (speed * speed);
             float B = 2 * Vector3.Dot(D, V);
             float C = D.sqrMagnitude;
             if (A >= 0)
             {
                 Debug.LogError("No solution exists");
                 return target.transform.position;
             }
             else {
                 float rt = Mathf.Sqrt(B * B - 4 * A * C);
                 float dt1 = (-B + rt) / (2 * A);
                 float dt2 = (-B - rt) / (2 * A);
                 float dt = (dt1 < 0 ? dt2 : dt1);
                 return target.transform.position + (V * dt);
             }
         }
 
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
avatar image
1

Answer by DesignPie · Mar 27, 2016 at 02:03 PM

Unity does have a good way to calculate such physics. Presuming that you are new to Unity, Unity has a neat script called a 'Collider'. Go into your project, locate the inspector, click 'Add Component', and then search 'collider' and select your desired shape for your collider (there're tons!).

Add a collider script to both your arrow prefab and your enemy prefab.

Assuming that you want your arrow to be affected by physics, add the 'Rigidbody' script to it.

To test it's velocity, you must use the OnCollisionEnter function coupled with some code, it will look somewhat like this;

 void OnCollisionEnter(Collision collision) {
         if (collision.relativeVelocity.sqrMagnitude > 2)
             //Executes code if squared velocity is more than 2
     }
 

Here, you are using the OnCollosionEnter function (broadcasted once your collider hits something) which returns a Collision (in this case, we name it collision). Then we check the collision's relative velocity (a Vector 3) and then we simply get it's squared Magnitude (the sqrt function is quite memory-expensive) and compare it to two or whatever you like. For me, I just play around with the limit variable until I find out it's perfect.

Hopefully that helps and good luck with your game ;)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My tower defense script fires too often 5 Answers

Showing Tower's range in game 3 Answers

2D 360 degress platformer example needed 0 Answers

An another problem - Tower Defense GUI ver.2. 1 Answer

What is the procedure of spawning enemies in each round ? TD games 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