• 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 Alex_Gray · Aug 06, 2012 at 02:01 AM · aimtower defense

Formula to calculate a position to fire at?

Edit: Is there a formula that calculates the position a bullet would meet an enemy depending on the bullet's speed and the enemy's speed and direction?

In example: Let's take a 2-dimensional grid (x,y) and there is a tower at (1,1) and a enemy at (4,2), the enemy lies within the tower's range so the tower fires a bullet. The bullet travels at a speed of 3 (Units/Sec). The enemy is traveling at a speed of 2 (Units/Sec) at a 52 degree angle... Where would the tower have to shoot the bullet in order to hit the enemy at it's current velocity, while taking into consideration the bullet's speed?

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

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Seth-Bergman · Aug 06, 2012 at 06:50 AM

 void CalculateAimPos(Vector3 targetPos, Quaternion targetRot, int targetSpeed, Vector3 myPos)
  {
 Vector3 aimPos = (targetPos + myPos) * .9;
 desiredRotation = Quaternion.LookRotation(aimPos);
  }

I believe it's as simple as that

of course as far as I can see here you are not passing the (aimPos) to the bullet yet, to know where to end up.. I assume you are planning this..

Bigger problem though, is that all your declarations are in javascript! but this looks to be a c# script.. to declare your vars, change it to:

  Vector3 targetPos = myTarget.position;
  //Find target's rotation
  Quaternion targetRot = Quaternion.LookRotation (myTarget.position - transform.position, Vector3.up);
  //Find target's speed
  int targetSpeed = (int) myTarget.gameObject.GetComponent().mySpeed;
  //Find tower's position
  Vector3 myPos = transform.position;

hope this helps

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 Alex_Gray · Aug 08, 2012 at 11:50 PM 0
Share

Yes I changed all of the "var" but the problem with the first part:

void CalculateAimPos(Vector3 targetPos, Quaternion targetRot, int targetSpeed, Vector3 myPos)

{ Vector3 aimPos = (targetPos + myPos) * .9; desiredRotation = Quaternion.LookRotation(aimPos); } well first off it'd be (targetPos - myPos), but the enemy has a set speed, so if it went higher, the bullet wouldn't hit the enemy on time... and the bullet has a speed that can change as well. so what I'm asking is there a formula to find the Postion an enemy will be at given it's own speed and direction while also taking in consideration the bullet's speed?
  • ‹
  • 1
  • 2

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

2D Cursor Aiming 2 Answers

Getting the face direction 2 Answers

Pointing 2 empty game objects towards one point? (Raycast?) 1 Answer

addforce question for android? 0 Answers

Airplane Control, Aim ... Lerp and Vectors 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