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 /
This question was closed Apr 04, 2014 at 03:14 PM by felixpk for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by felixpk · Nov 14, 2013 at 01:12 PM · aimingtargetingtower defense

TowerDefense Game, shooting at Target!

Hey there, i know this question has been asked a few times here, but none of these answers helped me. ![][1]

I have the following information:

float **speed** = target.GetComponentInChildren().getCurrSpeed();

Vector3 direction = target.GetComponentInChildren().getCurrDirection();

Vector3 enemyPos = new Vector3(target.position.x,target.position.y,target.position.z);

Vector3 towerPos = new Vector3(transform.position.x,transform.position.y,transform.position.z);

Vector3 LookAt = enemy - tower;

Is there a simple way of doing the math? Thank you!

help1.png (424.6 kB)
help1.png (424.6 kB)
Comment
Add comment · Show 6
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 robhuhn · Nov 14, 2013 at 01:15 PM 0
Share

What do you need to know? What math?

avatar image felixpk · Nov 14, 2013 at 01:21 PM 0
Share

I need to make sure the bullet hits the Enemy. So when I use LookAt he aims directly on the enemy and the bullet fails, so i have to aim a little bit in front of the target, relative to the direction of the target and the speed of the target.

avatar image robhuhn · Nov 14, 2013 at 01:25 PM 0
Share

Is the movement of the bullet linear with a constant speed or does it decelerate?

avatar image felixpk · Nov 14, 2013 at 01:33 PM 0
Share

no, it is linear, it just translates along the z axis with currentSpeed and Time.deltaTime

avatar image felixpk · Nov 14, 2013 at 02:50 PM 0
Share

Thank you Robhuhn! It is working perfectly!

my code is like this:

 float bulletSpeed = 20;
             float enemySpeed = target.GetComponentInChildren<EnemyScript>().getCurrSpeed();
             
             Vector3 enemyDirection = target.GetComponentInChildren<EnemyScript>().getCurrDirection();
             Vector3 enemyPos = new Vector3(target.position.x,target.position.y,target.position.z);
             Vector3 towerPos = new Vector3(transform.position.x,transform.position.y,transform.position.z);
             
             float distance = Vector3.Distance(enemyPos,towerPos);
             
             float tBullet = distance / bulletSpeed;
             float sEnemy = enemySpeed * tBullet;
             
              enemyPos += enemyDirection * sEnemy;
             
             top.LookAt(enemyPos);

Thank you very much you helped me alot!

Show more comments

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by robhuhn · Nov 14, 2013 at 01:47 PM

The easiest calculation I can think of is an approximation of the aim position. I didn't do any tests but it should be something like:

  • find the time the bullet needs to reach the target (tBullet = sBullet / vBullet)

  • find the distance the enemy can travel in that time (sEnemy = vEnemy * tBullet)

Now because the tower rotates to that position sBullet will change and so tBullet does. You would need to repeat that calculation for more accuracy, but I guess it will be good enough if you execute the calculation only once.

Don't know if there is an easier way.

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 felixpk · Nov 14, 2013 at 01:51 PM 0
Share

Thank you, I will give it a try and post the result here!

avatar image robhuhn · Nov 14, 2013 at 03:04 PM 0
Share

Glad that it works but be aware that the distance you add to the enemy direction should give the position on the path. That way it will also hit if the enemy moves around a corner. Now you assume the enemy walks straight

 enemyPos += enemyDirection * sEnemy;

Say the path has a total of 20 meter and the enemy is at 10 meter (0.5 ratio) in front of a corner. The aim position should be 1 meter ahead, then get the position on the path at a ratio of 0.55 (11 meter)

avatar image
0

Answer by ma_tech · Nov 14, 2013 at 02:07 PM

You can combine your solution with a guided missile which automatically follows the target. So you can be 100% sure, that your missile hits the target, even how komplex, tortuous or chaotic your path is.

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
0

Answer by gajdot · Nov 14, 2013 at 02:06 PM

Usually tower defense games cheat instead of really calculating. If your bullet is fairly fast, they usually just put a script on the bullet to follow it. Something like this: transform.lookAt(enemyPos);

And move the object forward or add velocity to the rigidbody of the bullet if any. And add maybe a destroy condition, if the bullet traveled too far from tower in case you have enemies witch are too fast to shoot at.

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 felixpk · Nov 14, 2013 at 02:39 PM 0
Share

I thought of that before, but I also have a $$anonymous$$issleTower, the missles really use LookAt(targetPos) to never fail, but this is really not what I want to on my BulletTowerScript :(

avatar image gajdot · Nov 14, 2013 at 02:50 PM 0
Share

I see, but now I'm a bit confused. If you want to make sure that your bullet hits, then you just need to speed it up a bit and hit a bit in front of your enemy, then use the same lookAt or lerp method after it traveled a bit, this way you hit it anyway. Otherwise if you need to miss your target time to time just use something like robhuhn suggested. That will miss your target in corners anyway.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How to make a targeting system to target an enemy furthest on the track 1 Answer

My Tower is acting weird (shooting at one enemy, killing different enemy) 0 Answers

Shoot where to camera direction ? 3 Answers

Limit local rotation around x, y, and z axes? 0 Answers

Slow Character Movement 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