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 Kristopher Williams · Feb 17, 2011 at 10:55 AM · javascriptfpsdistance

Distance on fps

Im trying to create a readout onto a gui so that when the crosshair is pointed at a object then it feeds out distance to that object. Its for sniping fps. I already know how to do the gui so if anyone can help with the distance calc from camera to object thatd be great. The main problem im havin is working out on the fly which object is being looked at. Thanks for any help anyone can give me. oh btw prefereably in java.

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 Kristopher Williams · Feb 17, 2011 at 11:54 AM 0
Share

I have now implemented colliders on the targets within the scene. And tried the code from rudolfwm. However is there a way to do it to centre of screen rather than mouse postion?

avatar image rudolfwm · Feb 17, 2011 at 12:18 PM 0
Share

yes there is: Just use: Camera.main.ScreenPointToRay (Vector3(Camera.pixelWidth/2,Camera.pixelHeight/2,0));

3 Replies

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

Answer by rudolfwm · Feb 17, 2011 at 11:21 AM

You need to have a collider on your objects.

If thats the case you can use a ray from the camera to see if its intersects.

var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit, 100)) { Debug.Log( "Hit something at:"+hit.distance); }

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
1

Answer by TheDemiurge · Feb 17, 2011 at 11:19 AM

Is there a maximum distance at which targets should no longer be recognized? There are a couple of ways you could do this. The best I think would be to raycast a few times per second (not every frame, waiting .3 second isn't that bad for this kind of 'readout') from the camera to infinity (or your max range) in that direction. Pull up the docs and look up Camera.ScreenPointToRay, that will get you started. This function takes a point in screen space (Z is ignored) and gives you a Ray, that starts at that spot on the camera's near clipping plane and goes in the camera's forward direction. You would then use Physics.Raycast to do the distance check. Look for the version(s) of Physics.Raycast that also take a RaycastHit as input. From that you check which collider or tag you hit, and if it's something appropriate, the actual collision point is stored inside the Raycasthit variable, and the distance check is a simple (VectorB-VectorA).magnitude check.

You can use the layer collision matrix if you want to be able to 'target' the baddies through things like bushes and the like. Everything that should not be considered an obstacle or a baddie should go on the IgnoreRaycast layer.

Once you've got this working you can also take things a step further if you want, and outline or highlight targets in various colors based on whether they're friendly, hostile, or any other criteria for targets and environment pieces you're targeting - or alert them that they're being targeted, etc. You've already got a reference to the GameObject in question.

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 TheDemiurge · Feb 17, 2011 at 11:24 AM 0
Share

Well, my answer is also useless if you're not using any colliders. Is there any way you could add colliders and use that layer collision to avoid causing issues with your current functionality? otherwise the easy way is to start manually checking cube collision with a bounding box for each object and an invisible 'stick' attached to your camera.

avatar image
0

Answer by _MGB_ · Feb 17, 2011 at 11:11 AM

If your objects have colliders, see this question for some help. Otherwise it's more complex...

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The next problem with gui. 1 Answer

How do I make an enemy lead his shots? 2 Answers

Free FPS Character Controller? 1 Answer

How to conect distance with animation? 0 Answers

Hurting and getting hurt 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