- Home /
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.
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?
yes there is: Just use: Camera.main.ScreenPointToRay (Vector3(Camera.pixelWidth/2,Camera.pixelHeight/2,0));
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); }
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.
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.
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...
Your answer
Follow this Question
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