I’ve been trying to setup my menus to be within a world space canvas for a while now. It’s an FPS game and I’ve properly locked all movement while in the inventory and enabled mouse movement but I’ve had numerous issues actually getting it working.
My latest attempt has produced a new set of issues that has me and the people helping me stumped. I am trying to get a custom mouse pointer image to follow the mouse position in the menu.
Here is the latest batch of code, placed on the mouse pointer’s 3d image as a child of the canvas, which is a child of the PDA object.
using UnityEngine;
public class PDAMouse : MonoBehaviour
{
public RectTransform PDAMouseImage;
public Vector3 offset;
public GameObject PlayerPDA;
public Camera cam;
void Update()
{
MoveObject();
}
public void MoveObject()
{
Vector3 pos = Input.mousePosition + offset;
Collider pdaCollider = PlayerPDA.GetComponent<MeshCollider>();
Ray mouseRay = cam.ScreenPointToRay(Input.mousePosition);
Vector3 targetPos = Vector3.zero; // the position that the pointer will default to if ther raycast doesn't hit
if (pdaCollider.Raycast(mouseRay, out RaycastHit hit, float.PositiveInfinity))
{
targetPos = hit.point;
}
PDAMouseImage.position = cam.WorldToScreenPoint(targetPos);
}
}
The latest issue has the mouse gameobject, for whatever reason, be well out of bounds of the entire map even though it seems to be follow mouse movements. Here’s a video of the issue (the game object is selected, and is the object you see moving on the top screen)
And an image of the canvas on the PDA, so you can see it’s slotted in and sized fine, along with the mouse image in its starting position:
I would be extremely appreciative for any help here.