I'm attempting to be able to "target" a 3D object in-game. For example, let's say my game is a 3D field with a moose standing in it. When I click the moose with my mouse (no pun intended), I want the game to realize that he has been selected and show "A Moose" in a part of the game's interface. I'm not worried about the interface, I just cannot figure out how to take 2-dimensional mouse coordinates and decide whether an object is clicked in a 3-dimensional world. Obviously the distance from the object greatly affects the way it is selected, because the farther you are from the object, the smaller it is, and ultimately you have a smaller "hotspot", so to speak. I attempted this myself and it went very poorly. I could turn away from the moose and click anywhere on the screen and it would detect that I'd clicked the moose. This is because of the mouse coordinates. It doesn't matter which direction you're facing, the coordinates will always be the same. Here is how I presume it could be done, but I'm not sure, so I'm asking for assistance:
Now I know the code above is probably way off, but this is why I need your help Anyone?
Code:
'This is not a language, just easier to explain!
'This is how far the moose is
DistanceFromMoose = Moose.Z - Me.Z;
'This is how much space is on each side of the moose
'Just to make sure I can actually see him on my screen
BlankScreenRight = (Screen.Width - Moose.X) + Moose.Width;
BlankScreenLeft = Screen.Width - Moose.X;
'I know these are wrong.. my head hurts :(
HotSpotWidth = Moose.Width \ DistanceFromMoose;
HotSpotHeight = Moose.Height \ DistanceFromMoose;
HotSpotY = Screen.Height - DistanceFromMoose;
'Try to see if we've clicked him
if (Me.X >= (Moose.X - BlankScreenLeft) and Me.X <= (Moose.X + BlackScreenRight)) {
if (ClickX >= Moose.X and ClickX <= (Moose.X + HotSpotWidth) and ClickY >= HotSpotY and ClickY <= (HotSpotY + HotSpotHeight)) {
'The moose was clicked
}
}