I am trying to detect the position of the mouse and create a ray. If it detects a gameobject that has the “Tile” tag and that same gameobject has been clicked twice, it will delete it. However, I’ve noticed that the mousePos variable is always the same, and also that it is the exact same as my camera’s transform
using UnityEngine;
public class TileClick : MonoBehaviour
{
private Vector3 mousePos;
private RaycastHit2D hit;
private GameObject prevTile;
private GameObject tile;
private void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Debug.Log("mouse clicked down");
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
TileClicked();
prevTile = tile;
}
}
private void TileClicked()
{
hit = Physics2D.Raycast(mousePos, Vector2.zero);
Debug.Log(mousePos);
if (hit.collider != null)
{
Debug.Log(hit.collider.gameObject.name + " has been hit");
if (hit.collider.gameObject.tag == "Tile")
{
tile = hit.collider.gameObject;
}
}
if (prevTile!=null)
{
if (prevTile == tile)
{
Destroy(tile);
}
}
}
}
I’ve tried changing Camera.main to a serializable camera variable, but it still does not fix this issue. My camera also has the “MainCamera” tag
That’s a complete non-sensical fix. You should read the documentation of ScreenToWorldPoint. The screenspace position’s “z” value should indicate the desired distance from the camera. Using the worldspace z position of the camera relative to the world origin makes no sense at all. The further the camera is away from the world origin, the further away the projected point would be from your camera. When you use 5f for the z value, the resulting point would be 5 units away from the camera.
Keep in mind that a single screen point maps to infinitely many points in worldspace. A while line / ray. You have to tell it which distance you actually want.
It’s not quite clear what your setup is, what kind of camera you’re using (perspective or orthographic) and what your goal is. Since you use Physics2D it seems to be a 2d game / environment. So you usually would have an orthographic camera. So the distance from the camera shouldn’t matter at all. If you use a perspective camera, you need to pass in the desired depth / distance from the camera.
Also using a 2d raycast with no direction doesn’t make much sense. This sounds like you wanted to use Physics2D.OverlapPoint.
I’m thinking that must be GPT-3.5. GPT-4 tends towards being much more aware of what the code is trying to do.
GPT-4’s Answer
The issue with your mousePos variable being the same as the camera’s transform is because you are not accounting for the depth (z-axis) when converting from screen to world point. Your current code uses Camera.main.ScreenToWorldPoint(Input.mousePosition), which does not include the proper depth information.
To fix this, you need to add a depth value when converting the mouse position to world coordinates:
However, since you are using Physics2D.Raycast, it is better to keep the z-value at zero. So, you should adjust the z-value of the mousePos variable before using it for raycasting:
mousePos = new Vector3(mousePos.x, mousePos.y, 0);
Unfortunately this is one of those situations where the bot just isn’t going to be helpful. You can get hints that the camera might not be set up correctly if you ask it if there is anything else that might be causing the problem but unless you get lucky with a hallucination (ie it’s guessing) it’s not going to mention orthographic vs perspective.