playMaker

Author Topic: Raycast to center of screen from object?  (Read 5116 times)

plinko

  • Playmaker Newbie
  • *
  • Posts: 11
Raycast to center of screen from object?
« on: September 11, 2016, 05:15:21 AM »
Hello, I'm trying to make a raycast from a gun hit the center of the screen, but it is firing in seemingly random directions for some reason. This is in the gun, in an FSM called "Get Screen Center", State 1:





This then flows into State 2 which just has Next Frame Event back into State 1. ScreenCenterVector is global, by the way.

Then my raycast does this in another FSM on the same object called "Shooting":


Every time I hit play, the direction is different and I'm not sure why. I think I may be doing something wrong. Any help is greatly appreciated!

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Raycast to center of screen from object?
« Reply #1 on: September 11, 2016, 05:30:12 AM »
is this in 3D? I'd raycast forward from the camera and get the target position that way, screen position converted to world point would be missing info on one of the axis

plinko

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Raycast to center of screen from object?
« Reply #2 on: September 11, 2016, 05:56:14 AM »
is this in 3D? I'd raycast forward from the camera and get the target position that way, screen position converted to world point would be missing info on one of the axis

Oh sorry, yes it's 3D. So now I've done raycast from the camera and stored the hit point in ScreenCenterVector and disabled the other stuff. Still firing in strange directions though? It seems like it's "following" the hit point, just incorrectly. Like if I move it over various objects, and do it over and over, it always fires at the same direction, but that direction is wrong if that makes sense? I'm heading to sleep for now but tomorrow I'll be back!
« Last Edit: September 11, 2016, 06:13:29 AM by plinko »

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Raycast to center of screen from object?
« Reply #3 on: September 11, 2016, 07:35:09 AM »
oh I hadn't looked at your raycast, yeah, direction and position are different things

you'd need to convert it, if you're lazy like me you might just use Look At to point the source at the target and then raycast with the direction 0 0 1

plinko

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Raycast to center of screen from object?
« Reply #4 on: September 12, 2016, 12:03:54 AM »
Hmm is it camera position - ScreenCenterVector? I remember seeing something using subtraction to find it but I can't seem to get it to work.

plinko

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Raycast to center of screen from object?
« Reply #5 on: September 14, 2016, 12:23:25 AM »
Hello again, does anyone have any ideas? I tried what I said above but it didn't work out so well.

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Raycast to center of screen from object?
« Reply #6 on: September 14, 2016, 05:00:59 AM »
forget screen center, its whats throwing you off

raycast from the camera, with the direction as 0 0 1. store the hit point

use vector3 operator to subtract the source position from the hit point position (target - origin). store the result as direction

raycast from the source with the direction

plinko

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Raycast to center of screen from object?
« Reply #7 on: September 14, 2016, 11:21:03 PM »
Thank you for the info! I'm glad it seems to be working, but now the crosshair is not matching up with the raycast location.



I'm using Set Mouse and then Hide Cursor and Lock Cursor. Is the crosshair incorrect?

EDIT: It does appear to be wrong. I used photoshop to check the center, and the green dot is the center:


How would I fix that?

EDIT 2: Also I didn't really want to ask because I feel like I'm asking too much but...I'm also trying to make the raycast have random spread like a real weapon, and it worked before I centered the raycast with all this, but now I'm not sure how to start at the center and then randomize the raycast direction slightly every time it fires. I tried creating 2 random floats that then go into a vector 3 and then adding that to the Center vector3 but it's not random. Sorry to ask so much but this would be nice to know as well!
« Last Edit: September 15, 2016, 02:37:41 AM by plinko »

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Raycast to center of screen from object?
« Reply #8 on: September 15, 2016, 05:46:36 AM »
Could it be the cursor sprite? Not sure about that one.

For the spread, instead of raycasting at 0 0 1, you can add a random float to X and Y. You'll probably want to keep it low, even 0.01 should make a difference