• 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 ForceX 1 · Mar 25, 2011 at 10:36 PM · targetcrosshair

Calculating Lead for a target (Not a rigidbody solution)

I've read many post on this matter and just about all of them refer to rigidbody.velocity. In my case i am not using rigidbodys or physics. What i am attempting to do is display a GUI that indicates where the player should shoot in order to hit the target. Here is what i have atm.

private var Player : Transform; private var SelectedTarget : GameObject; var TLI_HUD : GUITexture;

function LateUpdate(){

var TargetSpeed : float = SelectedTarget.GetComponent(AI).Speed; var ProjectileSpeed : float = 200; TLILead = ((transform.position - SelectedTarget.transform.position).magnitude * (TargetSpeed)) / ProjectileSpeed;

 //Set GUI with this

TLI_HUD.transform.position=PlayerCamera.camera.WorldToViewportPoint(SelectedTarget.transform.TransformDirection(Vector3.forward) * TLILead);

 //Or Set GUI with this

//TLI_HUD.transform.position=PlayerCamera.camera.WorldToViewportPoint(SelectedTarget.transform.position + (SelectedTarget.transform.forward * TLILead));

}

Any thoughts, ideas, or other solutions to make this work are welcome.

Comment
Add comment · Show 1
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 Bunny83 · Mar 26, 2011 at 12:01 AM 0
Share

Your last line don't make much sense because you convert the offset vector from world into viewport space, but it's not a world space coordinate. You have to add at least the target position

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Mar 25, 2011 at 11:57 PM

The calculation of the exact interception course of two linear moving objects is quite heavy. I've done that one time in my life. The approximation you use will work if your projectile is a lot faster than the target you try to hit. You can improve the accuracy by repeating the calculation you did with the new target position. It will be only an approximation and you will never get the correct angle, but it comes quite close.

var TLI_HUD : GUITexture;

private var Player : Transform; private var SelectedTarget : GameObject; private var iterations : int = 3; // increase this to get a better result private var ProjectileSpeed : float = 200;

function CalculateLead(target : Vector3, targetSpeed : Vector3) : Vector3 { var dist : float = (Player.position - target).magnitude; var timeToTarget : float = dist / ProjectileSpeed; var newTargetPosition : Vector3 = SelectedTarget.transform.position + targetSpeed * timeToTarget; return newTargetPosition; }

function LateUpdate () { var TargetSpeed : Vector3 = SelectedTarget.transform.forward * SelectedTarget.GetComponent(AI).Speed; var aimTarget : Vector3 = SelectedTarget.transform.position; for (var i = 0;i<iterations;i++) aimTarget = CalculateLead(aimTarget,TargetSpeed); TLI_HUD.transform.position=PlayerCamera.camera.WorldToViewportPoint(aimTarget); }

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 ForceX 1 · Mar 26, 2011 at 03:20 AM 0
Share

I plugged in your code, but i'm still getting the same results as before. The TLI_HUD GUI Texture stays fixed on the center of the SelectedTarget. If you have a joystick you can see for your self here. http://dl.dropbox.com/u/11090933/AI%20Test/WebPlayer/WebPlayer.html To select the target press T on your keyboard. Due to scale the target ship's speed is .13 and the projectile speed is 200. Even changing the projectile speed down to 10 does not seam to effect any thing.

avatar image Bunny83 · Mar 26, 2011 at 03:39 AM 0
Share

Have you checked whether SelectedTarget.GetComponent(AI).Speed returns the right value? Also check the magnitude of (aimTarget - SelectedTarget.transform.position) to see if there's a difference. If every variable is assigned it should work. Can't test it at the moment (it's 4:40am here :D) maybe later.

avatar image ForceX 1 · Mar 26, 2011 at 04:00 AM 0
Share

Thank you much for your help in this. I'll check those values and get back with you tomorrow.

avatar image
0

Answer by ForceX 1 · Mar 26, 2011 at 04:32 PM

@Bunny83 I got it all working. As it turns out i was accessing the Vector3 and rotation of a child object attached to the SelectedTarget, and it's rotations were not changing even though the parent was rotating. I made a few changes and came up with this, which works just fine for now.

You can check out the updated WebPlayer HERE

var Player : Transform: var SelectedTarget : GameObject; var TLI_HUD : GUITexture;

function LateUpdate(){ var dist : float = (SelectedTarget.transform.position - Player.position).magnitude; var TTT : float = dist / (ProjectileSpeed); var TargetSpeed : Vector3 = (SelectedTarget.transform.parent.forward (SelectedTarget.GetComponent(AI).Speed) TTT); TLI_HUD.transform.position=PlayerCamera.camera.WorldToViewportPoint(SelectedTarget.transform.parent.position + TargetSpeed ); }

Thanks again for your help with this.

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.

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

1 Person is following this question.

avatar image

Related Questions

Crosshair drawn at z-coordinate in front of TP player - not centre of screen 0 Answers

How to create lock on UI element when trackable detected? 1 Answer

Crosshair? How? 6 Answers

2.5D weapon aiming with cross hairs help needed 1 Answer

Unity3D Fps Realistic Crosshair. 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