Visual Script: Make Your Reticle Follow Mouse (3D Games & Input System Package)

First, we need to add a new action to get the coordinates of the mouse. Copy-pasting under Look the delta mouse, and change that delta to position.

  1. Project Settings > Input System Package > Actions +

I used name “MouseAim” because thats what it does in my game. Where the mouse is, the character aims.

  1. Copy under – Look ▼ “Delta [Mouse]” – and paste it under your new action.
  2. Change Path from Delta to Position.

  1. Regenerate Nodes, by going to Visual Scripting Settings (below in this window)

Once you do this… You can start your Script Graph.

I have my Canvas strecth to my screen. And there in UI there’s the playerReticle

Before we go into script… here’s the logic. Your camera shoots a laser, and where it hits - it gets the position of the mouse in the world. So we need to call our camera position, and cast a ray and see where it hits using a layer. In my case, i have big invisible cubes to maintain characters in the world.

Yes, my game is 2.5D world. You can move up and down in Z. So these invisible walls maintain players inside the world. This allows me to expand into other types of worlds in the future. Anyways…

To the Visual Script Graph:

Use this “MouseAim” to get the mouse position coordinates and via raycast + Layers of the world + WorldToScreenPoint to transfer that into numbers your UI Reticle can use to follow the mouse position.

Here’s a link to a .Gif showing the results… I couldn’t upload gif in this forum… meh

Anyways! enjoy! This took me a while… many days, different moments, lots of headache… but its done!

P.D. I don’t want to learn c#. I like visual script. Anyways, I got this from surfing online and found this code to make this happen… 2D games are easier to get mouse position on screen. 3D games needed a tweak like this… so here’s the original C# in case you want it (or study it)

public class MousePosition3D : MonoBehaviour
{
[SerializeField] private Camera mainCamera;

private void Update()
{
    Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out RaycastHit raycastHit))              <-- my struggling point is this
    {
        transform.position = raycastHit.point;
    }
}

}

1 Like

Privacy & Terms