1
\$\begingroup\$

I've built a turn based battle combat game (like pokemon) but i'm stuck between a rock and a hard place.

Asfar as i understod UI elements should be inside Canvas, while Players, Enemies, Items and etc... Should not be inside a canvas. Correct me if i'm wrong.

I've tried different ways how to handle this situation. Such as make Enemy as a button, but seems not to fire. Since i think it has to be inside a canvas. Also make enemy which have animations as button, may not be a good idea?

Update event, to check if the mouse is down and mouse position is converted with screentoworldpoint and see if the positions matches with the enemy position.

Update event, to check if the mouse is down and rayCast see if we colide with something. But i dont feel like this is neccesary since the enemy does not move. So the position is always the same.

Also if it's possible to take into consideration that every enemy is different size. But this is a minor detail, i would like firstly tackle the best way handle onClick event without the extra overhead.

I would appericate any feedback on my dilemma.

\$\endgroup\$
9
  • \$\begingroup\$ Is your Enemy 2d or 3d? \$\endgroup\$ Mar 22, 2020 at 13:48
  • \$\begingroup\$ I forgot to metion it's a 2d game \$\endgroup\$
    – Printer
    Mar 22, 2020 at 13:49
  • \$\begingroup\$ What about this? youtube.com/watch?v=3NBYqPAA5Es \$\endgroup\$ Mar 22, 2020 at 13:53
  • \$\begingroup\$ Button is inside a Canvas, so events are getting trigged, if i heard correctly only UI elements should be inside a canvas. \$\endgroup\$
    – Printer
    Mar 22, 2020 at 14:00
  • \$\begingroup\$ "static" has some very particular meanings that might not apply to your case here. Can you explain what you're using this word to mean? A screenshot of what your game should look like, highlighting the clickable parts, could also be a big help to get good answers for your use case. \$\endgroup\$
    – DMGregory
    Mar 22, 2020 at 14:03

1 Answer 1

1
\$\begingroup\$

If you are trying to allow clicking on an object in the world, as compared to a UI element, then raycasting is the way to go.

There are two ways to do that:

1) Add a collider to the Enemy's GameObject and add the following script somewhere:

public class MouseHandler : MonoBehaviour
{
    public Camera theCamera;

    void Update()
    {
        Ray cameraRay = theCamera.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(cameraRay, out Raycast hit))
        {
            Debug.Log("Clicked on " + hit.collider.name);
        }
    }
}

Use the LayerMask parameter of Raycast to limit what results it returns. You can then call hit.collider.GetComponent<Enemy>() to get the Enemy script on the object that was clicked.

Also: if your GameObject has a RigidBody, then you might want to use hit.rigidbody instead of hit.collider.

2) Put a collider on the Enemy's GameObject, a Physics Raycaster to the Camera GameObject, and add the following to the Enemy script:

public class Enemy : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            Debug.Log("Clicked " + name);
        }
    }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .