I’m trying to make a simple Point & Click movement system with raycasting, but whenever I click on the baseplate object, the raycast casts through the object and hits completely something else. I’m still new to Unity3D, and I have no idea why it’s doing this. I tried raycasting only to the baseplate’s layermask, but it didn’t solve the issue.
On the image attachments, the first image is before clicking the baseplate, and the second image is after clicking the baseplate (notice the position.)
If anybody could help me, that would be great!
Here’s the code:
public Camera cam;
// Start is called before the first frame update
void Start()
{
Debug.Log("Script running...");
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonUp(0)) {
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("Baseplate"))) {
Vector3 mousePos = Input.mousePosition;
transform.position = transform.position + new Vector3(mousePos.x, -1.44f, mousePos.z);
}
}
}