I had this working in 2D, but to get this to work right I have to do it in 3d. The idea is just to see if the mouse is over the enemy but it’s not working. The editor says hit.collider etc. is deprecated so I declared a public Collider2D object collider2d instead. But it doesn’t work.
If youre making a 3D game you use use 3D physics so use Collider instead of Collider2D, also your physics.raycast is not in an if statement? Is this even running? Sorry never seen it done like this before
i’m making a 2d game but using a 3d raycast to see what’s under the mouse (i.e. is the enemy under the mouse?). you’re right that i just replaced hit witch collider without connecting collider to physics.raycast. not sure what i should do.
There’s a couple of ways to do it. The simple way is with OnMouseEnter.
Another way is just raycasting from where the mouse cursor is into the world.
// Layer that enemy is on - remember to set this in inspector.
public LayerMask _layerMask;
// Distance to cast ray
public float _raycastDistance = 10000f;
void Update()
{
LogHoveredEnemyName();
}
void LogHoveredEnemyName()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Create ray from camera into the world through the mouse cursor position.
RaycastHit hitInfo; // Empty var to store info about the thing we hit
if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, _raycastDistance, _layerMask)) // If raycast hit a collider...
{
if (hitInfo.collider.tag == "Enemy") // Tag is enemy?
{
Debug.Log(hitInfo.collider.name); // Print name of enemy it hit.
}
}
else
{
// Not hitting anything
}
}
so if i wanted to use OnMouseEnter, i would put that in the enemy’s attached script and then use an event to signal the hero’s enemy_over script? i just learned about events (i guess i knew about them in java, but that was a long time ago).
edit: could this be the reason -
“Property collider has been deprecated. Use GetComponent() instead”
what should i do instead then?
still not working:
void CastRay()
{
//Vector3 mousePos = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Create ray from camera into the world through the mouse cursor position.
RaycastHit hitInfo; // Empty var to store info about the thing we hit
if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, 100)) // If raycast hit a collider...
{
if (hitInfo.collider.tag == "Enemy") // Tag is enemy?
{
Debug.Log(hitInfo.collider.name); // Print name of enemy it hit.
enemy_over = true;
}
}
else
{
// Not hitting anything
enemy_over = false;
}
}
enemy_over is not being triggered even when i click on the enemy. odd.
Yes. You’d need to have a script on every enemy with OnMouseEnter inside it, then send a static event. The script that is interested in that data would subscribe to it in OnEnable and unsubscribe in OnDisable.