playMaker

Author Topic: Trouble Placing Cross Hair Center Screen (also Raycasting from Center Screen)  (Read 895 times)

Nobody Important

  • Playmaker Newbie
  • *
  • Posts: 2
Hey! For some reason I just can't figure this out.

First, what is the best way to place a crosshair in the exact center screen?

Second, what's the best way to ray cast from center screen?

Bonus question:
I'm trying to figure this out because I an attempting to apply force to an object to launch it, and I want to to launch to center screen. Is there a method for doing this I am not aware of? For the purposes of the game, the way an object is launched is by using "Explosive Force" on the object to launch it outwards. I want to make it so it launches to a general location on screen (center screen).

Any help would be appreciated!! Thanks much!!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Hi.

Maybe this video can help :


daniellogin

  • Full Member
  • ***
  • Posts: 215
I didn't watch the video djaydino posted, but I'm sure it explains how to raycast from the center of the camera and that general side of things. Start there if you have never done raycast or used that to put bullet holes using the hit rotation, etc.

However, does it tell you how to shoot what is in the center of the camera (to line up with a crosshair) BUT fire a projectile from THE GUN, not camera center?
This is useful if you are going to do a particle or object to be the bullet or to add visual effect. It's not useful if you are simply insta ray hitting the exact spot. This may seem at first like a given, or you simply position your gun model to line up, but that won't work at all ranges- only 1 exact range and that's it. Also if you have the projectile appear from the camera, it looks weird because you clearly see the gun isn't putting it out.

OK so I asked myself the question recently, and this is basically what I did:

* Have object parented to camera (or otherwise using an FSM to update it's position to the camera center, etc) which fires a ray which corresponds to the crosshair on the center of the screen ("C Ray").

* Have an object positioned at the end of the gun barrel ("B Rotation")

* Have you sequence to fire the gun. This will include doing a ray from the C Ray, which stores the object and position hit.

* After that, get your B Rotation to Look At the position the C Ray hit

* Get Rotation for B Rotation.

* Pool spawn your projectile/particle from B Rotation position, and set it's rotation to B Rotation

And that's it... well except for how to get your projectile to fly and etc, but ask if you want help with that. Basically the above means your gun barrel position object is always looking at what your crosshair is, no matter what the range is (since it's adjusting rotation). This works well when I use it, and you likely won't notice that the projectile is not flying out exactly at the rotation of your gun model, especially if it's fast.

I actually added an extra layer to this so I could include a bit of random inaccuracy for weapons. This is done by all of the above, but having an additional object parented to B Rotation (B Spawn) which is where you will spawn from. The inaccuracy can be added by generating some random floats for X and Y, then doing a Set Rotation on B Spawn using those random floats. It's important to use LOCAL, not global rotation, so it is slightly altering the rotation of B Rotation (which is it's local 0,0,0 position), which of course is to correspond to your crosshair location. You only need tiny numbers to make a considerable difference, which of course amplifies with distance.

The only thing you will need to keep in mind is that you will need a way to ALWAYS hit an object, no matter where you look, or if you miss everything the shot will fire in a strange direction (which is towards the last actual object hit by your ray). One easy way to do this is to encase the whole map in a box, so you will always hit that box if nothing else. You could just get the did hit bool for the ray, but that would look weird if you shoot but no shot comes out, just because you fired at the sky, etc. 
« Last Edit: June 27, 2019, 01:58:10 AM by daniellogin »