How do you raytrace the mouse screen position onto a world XY Plane?

I wish to make a simple placement game where the user has to drop puzzle pieces.

I have managed to get screen coordinates based on the controller per visibility stack trace, neutralize the Z coordinate, and set the position of my test StaticMeshActor, Timmy. I pass him as a reference into Timmy Bariable which is what Set Actor Location sets. Then I print it for good measure.

What happens is that it works, but if my mouse cursor hits an obstacle I get…don’t fully get what, but I assume its some coordinates relative to the obstacle itself. I passed the mouse cursor over a little metal bar from the scene and Timmy started vibrating while moving on and off that metal bar.

What I assume its happening is that from the screen to the world, the trace line hits the metal bar and triggers the trace giving it messed up coordinates.

What I need is to make from screen to world trace but to a abstract plane of xy 0-2000, 0-2000 which are the bounds of my game. I have not found a way to pass an interval of vector coordinates. Please help, thank you.

Couldn’t you just make a Collision Channel/Object channel so your trace only hits what you want it to hit(such as the background)?

I can think of two options:

  1. Create a new trace channel in edit global settings; call it “placement”
  2. Put a new blocker volume in the world, set it to block “placement” and ignore all other tests
  3. Use this trace channel in the test of position-under-cursor

or:

  1. Do what you do now
  2. Normalize the vector you get (from camera to whatever position you hit) to length 1
  3. Project the vector to the plane where you want to get the location (divide by dot between vector and plane normal; multiply by distance-to-plane from camera center)

@jwatte I started playing with Unity yesterday. Your first suggestion seems like the proper direction, but I don’t understand any of your or Zeustiak’s information. Would appreciate some broader explanations. I will cherish you forever.

Go into project settings and create a new channel.

Then choose that for your line trace and make sure your object’s collision presets are properly set up.

Maybe this will help.
Define a trace channel:

Use the trace channel:

Forgot to annotate the first picture:

@Zeustiak. I have done this like in the annotated images but it only gives me slight variations like between 1 and 2. http://puu.sh/9vhxp/845e8366ba.jpg any ideas why?

@jwatte I have done this like in the annotated images but it only gives me slight variations like between 1 and 2. http://puu.sh/9vhxp/845e8366ba.jpg any ideas why?

How long is the ray you cast? Can you render an indicator where it hits in world space? (Some of the line trace nodes have this built-in)

@jwatte I somehow got it to work but the problem remains. Sometimes I get Z = 0 and it looks fine, Timmy, my marker is centered on the mouse. Sometimes the Z is not 0 and the x/y change as well and they flicker in the ok value set and the crappy one. I will make a video recording showing the coordinates and the cases it flickers. I can also archive my project and put it on dropbox if it would help.

In your screen shot, the collision volume is only in the middle of the screen. It needs to cover the entire world (and be invisible, and only collide with your trace channel and nothing else.)

@jwatte I only want a particulary sized area to fire ray collisions.

@jwatte Here is an archive of a simplified project. I want to detect the x and y only coordinates with z = 0 of that plane. Its fine to make it incredibly large because then I can just check if the result is within my allowed range. But for some reason it flickers sometimes between x0, y0, 0 and x1,y1,64 I played around with the box’s settings but to no avail. This is so frustrating… Dropbox - Error Open the level called Main

Line Plane Intersection blueprint node:

As far as getting inconsistent trace results, creating a new collision channel, placing a blocking volume that uses that channel and then tracing through it is one way. You can also try right clicking in blueprint and typing “plane” into the search field. There are two blueprint nodes for Line Plane Intersection that should do what you want without all the extra collision setup.

The nice part about using the collision option mentioned in the other answer is that you can create your collision volume to be the exact size of your gameplay space. With the below example, you can still do it, but you just need to take the final Intersection vector and do a check on the X and Y to see if it’s outside the bounds of your gameplay space (break the Intersection vector and use the float > and < operators or use the “In Range (Float)” blueprint node).

The two example images show finding the intersection of a ground plane (although you could change it to be any plane) with a vector line from the mouse and also from the center of the screen.

MOUSE to ground plane:
Convert Mouse Location to World Space, Make Plane and Line Plane Intersection.

Center of Screen to ground plane:
Get Viewport Size, Convert Screen Location to World Space and Line Plane Intersection (Origin & Normal)

To take your current example and bound it to 0-2000 in the X and Y, you would probably want to break the Location vector and then use either the (float) > and < operators or, better yet, the In Range (Float) blueprint node along with the AND and Branch blueprint nodes.

2 Likes

Maybe this will help:

Please help me, i don’t understand why my return value is always false!!!

I try with custom macro, with line plane intersection and line plane intersection (Origin & Normal) … but my result is always false…
If i try to print intersection … on screen give duplicate result (coordinates effective and coordinates 0,0,0)

Sorry for my english… i’m italian.

What’s wrong in my script??

If you disconnect this PrintString, do you see anything else printing to the screen,

no… but red sphere apperars fixed … if i try to assign location to white sphere, it lock on red sphere position…

The return value in Line plane intersection is always false…

If i try to execute from input keyb it work correctly…print only one string with correct coordinates… but in event tick the result is wrong… It read 2 vector and print for 1 cicle 2 different value

Thank you for your help

Sorry, by mistake I dragged the player controller on the world outliner, for this he execute the code 2 times …
Thanks very much for your interest anyway

Good job and good continuation to all :slight_smile: