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
0
Question by Mahtrok · Jun 03, 2013 at 10:28 AM · space shootercrosshairworldtoscreenpoint

Doing a Crosshair for a Space Fight Simulator

Yes i know there are plenty of questions answered for this kind of problem, but most of em do not seem to answer my question for i am still trying to get it running.

I am doing a space flight simulator where players connect and fight against each other, quite simple, i got nearly everything up and running except the crosshair and the velocity/bullet speed indicator to attack my target.

The crosshair should be shown always in front of my Ship for my weapons are pointing straight forward the camera should point to where my bullets are going. The velocity indicator should be shown at the position of the targets velocity Vector3 + the time my bullets need to be translated about the distance by speed.

I will not paste my code for it is totally not working and would be only the last try of many.

I tried DrawTexture, i tried a Label with the texture as value, i tried a GUITexture, i tried to get it running with camera.WorldToScreenPoint(Target.rigidbody.velocity bulletSpeed) -> GUIUtility.ScreenToGUIPoint, with camera.WorldToViewPortPoint by calculating a relative position of Target.position - transform.position, i used transform.TransformDirection(Vector3.forward distance) aso.

The last try was to addept a Script aldonaletto posted 2011 and its near the goal i am trying to reach but when i turn my ship to the left or the right the Crosshair moves ~100px to the left or right and returns to the correct position after stopping the movement, i do not get it. The following is the adepted code from aldonaletto.

     // targ is the target transform, vel is its velocity vector,
     // and speed is the projectile speed. Returns the time needed
     // to reach the target, or -1 if it's not possible
 
     float HitTime(Transform targ, Vector3 vel, float speed) {
       Vector3 dist = targ.position - transform.position;
       float a = Vector3.Dot(vel, vel) - speed * speed;
       float b = 2 * Vector3.Dot(vel, dist);
       float c = Vector3.Dot(dist, dist);
       float det = b * b - 4 * a * c;
       if (det >= 0)
         return 2 * c / (Mathf.Sqrt(det) - b);
       else
         return -1;
     }
 
     // that's an example code to test this function:    
     Vector3 targetVel;
     Vector3 lastPos;
     
     void DoHitMarker () {
       if (Target != null){ // if some target locked...
         // if you don't know the target velocity, calculate it
         Vector3 vel = Target.rigidbody.velocity;
 //        Vector3 vel = (Target.position - lastPos)/Time.deltaTime;            
 //        lastPos = Target.position;
         // filter the velocity to avoid a shaking crosshair
         targetVel = Vector3.Lerp(targetVel, vel, Time.deltaTime);
         float _bulletSpeed = RapidGunAmmoPrefab.GetComponent<Bullet>().Speed;
         // calculate the hit position:
         float t = HitTime(Target, targetVel, _bulletSpeed);
         if (t < 0){
           // target moving away too fast to be reached
           GUIHit.enabled = false;
         } 
         else {
           // target may be hit:
           GUIHit.enabled = true;
           // calculate the position where the target will be hit
           Vector3 hitPos = Target.position + t * targetVel;
           // calculate the crosshair position
           Vector3 crossPos = _cam.WorldToViewportPoint(hitPos);
           GUIHit.transform.position = crossPos;
         }
       }
     }
     
     void DoCrossHair () {
         _relativCrossHairPos = _cam.WorldToViewportPoint(transform.TransformDirection(Vector3.forward * 20));
         GUICrossHair.transform.position = _relativCrossHairPos;
     }

If anyone got an idea what i am doing wrong or a complete other suggestion please enlighten my mind for i am trying to get this running for at least 2 month now while everything else is running properly. I am using a rigidbody for my ship and AddRelativeForce(Vector3.back boost) to move my ship and simulate a bit of real physics, the bullets are moved by transform.Translate(Vector3.forward bulletSpeed) so the speed is calculatable.

Greetz Mahtrok

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 Mahtrok · Jun 04, 2013 at 08:20 PM 0
Share

Ok forget about it, i found my mistake by childing the GUITexture to my Ships transform. Now everything works fine.

0 Replies

· Add your reply
  • Sort: 

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

14 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

Related Questions

Crosshair Y-axis inverted 1 Answer

Question about a space shooter crosshair targeting script 1 Answer

Spaceship crosshair 1 Answer

Worldtoscreenpoint..... 2 Answers

screen coord 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