Update Rect Transform's crosshair to gun's raycast distance position

YukiYuki Posts: 46Member
edited July 17 in Questions
----- INITIAL QUESTIONS -----

Hi there. I have two simple questions about the weapons raycast :

1) How can I access the raycast properties for each weapons ? (I would need to attach a canevas 2D or 3D crosshair at the other end of it.)

2) How can I free the raycast so the weapon doesn't always shoot in the middle of the screen like it does no matter the Sway values ?
EDIT : I fixed this with the Vp_FP Weapon Shooter script setting the Rotation Pause to 0. (Yet the Muzzle Flash stays in a fixed position. I would create a new script and link this effect to the gun's canon.)

----- NEW QUESTION -----

How to make a canvas 2D or 3D crosshair follow the end distance screen position of a raycast attached to a gun ?

Answers

  • JustinJustin Posts: 298Member, Administrator, Moderator
    Take a look at vp_Shooter.SpawnProjectiles - this is where the direction is determined which you can then modify to point to your new crosshairs.

    [ UFPS Docs ] . . . [ F.A.Q. ] . . . [ Tutorials ] . . . [ Knowledgebase ]
    [ Games Showcase ] . . . [ Get it now! ]

    [home] [twitter] [youtube]
  • YukiYuki Posts: 46Member
    edited July 18
    I think this is not what I need Justin. Also my question was wrong and not clear.

    In fact, I would need to move this crosshair with the mouse (and joystick) axis, and make it go back to the screen center when the axis are untouched. It would have a dead zone so it doesn't goes off-screen. The Rotation Look Sway values of the gun would be dependant of that dead zone, so you don't aim off-screen, but you could still turn the camera around.

    When it comes to the gun's move (mainly made with Rotation Look Sway, Shake also) and aim moreover, I need the distance raycast (raycast is displayed in red) to follow the crosshair screen position as it moves with the mouse / joystick axis.

    Right now, the crosshair canvas doesn't go anywhere, and I need to figure out how to achieve what I explained in the two last paragraphs. Also, I guess I would need to convert the raycast distance, which is a world position (?), to the UI Canevas screen position.

    I bought the GameFlow asset to make things simple, but even with that I don't know what to really do despite hours of research.

    image
  • Zrexa Posts: 63Member
    The raycast always comes from the center of the screen (camera) out. There is no raycast attached to the gun. If you want your crosshair to sway when moving you could just determine whether your character is moving, running etc...(whatever state you want) then lerp your crosshair between predetermined positions. This probably requires some knowledge of coding, I highly doubt you can do all this with GameFlow. Although I have never used this asset, however, maybe it has the ability to move a ui element from point A to point B. But basically the easiest solution here is not for your crosshair to follow the exact gun sway (which has nothing to do with the raycasting anyway) but to have some artificial crosshair movement and then instead of raycasting from the center of the screen, raycast from the current crosshair position.
  • Zrexa Posts: 63Member
    edited July 18
    Alternatively if you want to add a raycast to your gun and move the crosshair over it (like you have in the picture above), disable the simple crosshair script, write your own, feed in the position of the current raycast hit and move the cursor to that point using camera worldtoscreenpoint
  • BrandSpankingNewBrandSpankingNew Posts: 2,317Member, MPTester
    edited July 18
    @Yuki , so you just want to move your crosshair around the screen without changing the camera's viewpoint, is that it? And then, if that is what you are asking, when the crosshair reaches the extent of the rectangle of the screen you would like for the camera to begin "following" the crosshair's movement and swing the players viewpoint in that direction? Is that it?

    So, for example, if you are running straight down a hallway and needed to make a quick left turn, then I assume that you would mouse or joystick your crosshair all the way to the left of the screen, with the weapon following that crosshair motion, and only when the crosshair had reached the full left extent of the current viewport rect would the player's body and camera viewpoint begin to turn in that direction, is that it?

    I don't really understand the part about making an "untouched" mouse cause the crosshair to come back to the center of the screen. I don't know how to make an "untouched" mouse position. Or, maybe you are talking about doing some kind of dual-control kind of scheme, where you drive the player with the joystick and maneuver the weapon with a mouse?

    I'm an expert at being confused, so don't think that it's your fault, but a little more "real world" scenario of how you would like this thing to work might be helpful. A picture is not always worth a thousand words.
    Beautiful objects are wrought by study through effort, but ugly things are reaped automatically without toil.
    Democritus
  • YukiYuki Posts: 46Member
    edited July 18
    @BrandSpankingNew : Yes, you understand what I need in your first paragraph.

    I said I need to make the crosshair going back in the middle of the screen when the mouse axis are standing still, not the opposite.

    @Zrexa : Why do you say there is no raycast attached to the gun if it's the case in my example ? And the gun sway not having to do with raycasting if the raycast is linked to the gun's canon ? I don't understand about raycasting from the crosshair's position. The idea of an artificial crosshair may be what I need, but I'm not sure ; it is already artificial anyway because it's a UI element, not a "real" world gameobject.

    On your second post, I think my raycast shouldn't hit anything as it has to be infinite and just provide screen point positions in X and Y axis.
  • YukiYuki Posts: 46Member
    I'm thinking about getting the same values than the "transform" copies created by UFPS in-game and make the crosshair acting just like them.

    image
  • YukiYuki Posts: 46Member
    edited July 18
    Here's what I achieved by using Set Rect Transform Property as "Achored Position" and a value of a Ray distance of 100 :

  • Zrexa Posts: 63Member
    edited July 18
    Based on your vid I think what you are looking for is take the raycast that comes from your gun and if it hits something, set the crosshair to that point - transformed to 2d space using camera world to screen point, if it doesn't hit anything you can set it to a point on the ray (ray.getpoint) very far away and transform that into 2d space again using camera worldtoscreenpoint.

    Here is the unity documentation on that function:
    https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html

  • YukiYuki Posts: 46Member
    edited July 18
    Nope ! I don't want this ray to hit anything, nor the crosshair to go to a hit point. The crosshair has to stay like it is.

    The issue I have here is that the distance doesn't seem to have a correct value. When I put it to 10 for example, the crosshair just goes slower. But it never goes to the correct position, at the end of the ray.

    Maybe I have to convert the raycast distance to a screen point like you said.
  • YukiYuki Posts: 46Member
    Here's what I achieved by converting the raycast's distance to a screen point and making the crosshair's Rect Transform follow that point.

  • BrandSpankingNewBrandSpankingNew Posts: 2,317Member, MPTester
    All that I see is your crosshair in the dead-center of the screen, as one would expect with a normal FPS, with the weapon swaying a little more than usual. Not at all as you said that I had described (see above). What "distance" are you talking about? What are you trying to do?
    Beautiful objects are wrought by study through effort, but ugly things are reaped automatically without toil.
    Democritus
  • YukiYuki Posts: 46Member
    edited July 20
    I'm simply trying to make a UI Rect Transform element going to a raycast distance (world position) converted to a screen point position, OR the opposite, make the raycast distance follow a crosshair.

    On the last video you can see well that the raycast's end and the crosshair are not on the same screen position ; one and another are not aiming at the same place ; thus the crosshair has no utility because the aiming is wrong.
  • YukiYuki Posts: 46Member
    edited October 26
    Hey,

    I managed to make something quite similar to what I wanted about this "Dynamic aim system".



    My initial project was to have the crosshair in 2D on a static canvas and link it to the screen position of the hit point raycast. Here, I simply made the canvas (set to World Position render mode on WeaponCamera) child of an Empty game object and the weapon itself on the canon's shooting line (raycast), while the rotation look sway does it's job.

    The result is satisfying with a joystick ; otherwise, I would like to change with the mouse, so that the rotation sway become slow while aiming and doesn't move back to the screen center too fast. I guess I have to tweak something into the weapon's states, but with the mouse only.

    Still, I wish I could achieve the 2D crosshair option...
Sign In or Register to comment.