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 /
avatar image
2
Question by The-Little-Guy · Aug 28, 2014 at 01:20 PM · c#rotationbullet

Predict where object will be

I am creating a top down turret defense game, and in those types of games when the turret shoots it always hits. What I am trying to do is calculate where an object will be based on the objects speed and the bullet speed so I can then rotate my turret to shoot at the correct location. If I point the turret directly at the object it will miss if the object is too far away.

My bullets/targets don't use AdForce/Velocity, I am using transform.Translate() and transform.Rotate() to move everything, and all of the formulas I found are using velocity as part of their calculations.

I found the following on reddit and tried my best to convert it to C#, but it doesn't work for me.

 float aimAngle(Transform go, float bulletSpeed){
     float speed = go.FindChild("Logic").GetComponent<Character>().speed;
     float v = go.rotation.z * speed;
     float rCrossV = go.position.x * v - go.position.y * v;
     float magR = Mathf.Sqrt(go.position.x * go.position.x + go.position.y * go.position.y);
     float angleAdjust = Mathf.Asin(rCrossV / (bulletSpeed * magR));
     
     return angleAdjust + Mathf.Atan2(go.position.y, go.position.x);
 }


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 jokim · Aug 28, 2014 at 01:24 PM 0
Share

Just a question, Is it a no-go to actually implement a ho$$anonymous$$g feature ? - I'm asking, cause it's actually simpler to achieve than the prediction one (but i'm not saying it can't be done)

avatar image The-Little-Guy · Aug 28, 2014 at 03:06 PM 0
Share

How would that work?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jokim · Aug 28, 2014 at 05:59 PM

Well, not directly answering your question, but here's how i'd approach this problem : (as I mentioned in a comment)

Have your bullet be a class. Which has a target that can be set. - most probably an enemy.

Now, instead of predicting where the target will be in X time, just go towards it. continuously. adjusting rotation every frame

When you create the bullet, give it a target, and in its update code, have something like :

 void Update()
     {
         transform.LookAt(target.transform);
         transform.Translate (transform.forward * speed);
     }

Just make sure your enemies never go faster than your bullets !

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 Doodlemeat · Aug 28, 2014 at 03:44 PM

I would say that a proper tower defense game doesn't allow this. Either make you turrets shoot faster or make the bullet/projectile ALWAYS moving towards the first enemy in line.(Homing)

Also, consider using velocitys to move things around. It doesn't make sense to "move" an object by changing the position manually. Let velocitys do that.

This is only true if you are using the FixedUpdate() instead of the Update(). Change to velocity if you want Update() But to answer your question. You know how fast the enemies are moving(units/second). You also know the speed of the bullet to the target.

float a = (Length between tower and enemy) / bullet_speed. (prediction of how long it takes for the bullet to get to the enemy).

float b = enemy_speed * time (this is the distance the enemy walks from when the bullet are fired and when the bullet reaches the last enemy destination).

Solve C! Pythagoras theorem.

a^2 + b^2 = c^2

EDIT: This is only calculated when the enemy walks in a straight line after the bullet has been fired. So if your enemy does make a turn after the bullet has been fired, this does not work.

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 jokim · Aug 28, 2014 at 05:55 PM 0
Share

Would you $$anonymous$$d pointing to some illustrations ? I don't see the Right angled triangle you're describing... (I'm actually interested in this topic)

EDIT - After re-reading, I can only conclude that this does NOT define a right angled triangle in EVERY cases. Have a tower at (0,0), enemy at (10,10), Now if the bullet reaches the last enemy position (10,10) but the enemy only walked to (5,10) : How is this a right angled triangle ?

(0,0), (5,10), (10,10) does not correspond... I can't do Pythagoras on that.

avatar image Doodlemeat · Aug 28, 2014 at 06:31 PM 0
Share

I´ve edited my answer now. I took for granted that his tower defense game contained straight walkpaths. The solution wont work with diagonal paths.

avatar image jokim · Aug 28, 2014 at 06:42 PM 0
Share

actually... even that restriction doesn't make all right angled triangles... $$anonymous$$y triangle was all straight paths. and it wasn't right angled.

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

Flip over an object (smooth transition) 3 Answers

2d game facing bullets to mouse 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Bullet flies away for no apparent reason. 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