Author Topic: Clicking on objects with mouse (Singleplayer)  (Read 571 times)

First, take a look at the following scenario:



See where the mouse is on the right side of the screen? I want to be able to click (already taken care of by binding something to the GlobalActionMap) and detect that it has clicked on the green brick. I've tried a few things, including finding the offset of the cursor from the center of the screen and retrieving an angle to fire a raycast at. However, this doesn't work in all scenarios - for example, in the above one, there are blue bricks blocking it from the centerpoint of the view.

Do any of you out there know how to do this? Remember that I can use both server and client functions since this would be ran in a singleplayer server, so feel free to reference anything you can. I think Nitramtj has done something similar, but I haven't been in contact with him since this problem arose. Also, unless it's a very educated one, please don't just throw a guess out on the table, since that won't help me nor other users that may be curious about doing this in the future.

See where the mouse is on the right side of the screen? I want to be able to click (already taken care of by binding something to the GlobalActionMap) and detect that it has clicked on the green brick. I've tried a few things, including finding the offset of the cursor from the center of the screen and retrieving an angle to fire a raycast at. However, this doesn't work in all scenarios - for example, in the above one, there are blue bricks blocking it from the centerpoint of the view.

Do any of you out there know how to do this? Remember that I can use both server and client functions since this would be ran in a singleplayer server, so feel free to reference anything you can. I think Nitramtj has done something similar, but I haven't been in contact with him since this problem arose. Also, unless it's a very educated one, please don't just throw a guess out on the table, since that won't help me nor other users that may be curious about doing this in the future.
I apologize if this seems uneducated or if you have already tried this, but you said you retrieved the angle for the raycast, but did you offset the starting position of the raycast by determining the offset of the mouse from the center of the screen along with the angle?

I apologize if this seems uneducated or if you have already tried this, but you said you retrieved the angle for the raycast, but did you offset the starting position of the raycast by determining the offset of the mouse from the center of the screen along with the angle?
That doesn't seem like a bad idea.

I apologize if this seems uneducated or if you have already tried this, but you said you retrieved the angle for the raycast, but did you offset the starting position of the raycast by determining the offset of the mouse from the center of the screen along with the angle?

I did try that, and if such is the solution that should make it work, I was certainly messing up somewhere along the lines with the math. (I only know what I've read on Google because I haven't taken trig yet in school; that's next year in my area's curriculum.) Since you don't have a 2D view of the world - you can see the red brick behind the green one on the left, but not the red one behind the green one straight ahead - you can't just offset it normally. However, you can see the brick the mouse is on, so there's probably some way to do this.