3
\$\begingroup\$

I'm currently facing this problem: If I click on a Button on my Canvas, the currently selected Unit moves towards the location under the Button.

Is is there any option to solve this? I'm using the A* Project for this.

Sadly, I can not provide a lot of things I've done so far as I absolutely have no clue how to solve this problem.

If I disable the Button Option "Raycast Target" it still moves to this position but holds a bit earlier.

Also, adding this as check didn't work:

if (!EventSystem.current.IsPointerOverGameObject() )

I also checked if the Raycast hits the layer of the GUI

My Movement code so far:

public override void MouseClick(GameObject hitObject, Vector3 hitPoint, Player controller)
{
    base.MouseClick(hitObject, hitPoint, controller);
    if (hasAuthority && player && player.human && currentlySelected)
    {
        if (hitObject.name == "Ground" && hitObject.name != "UI" && hitPoint != ResourceManager.InvalidPosition)
        {
            float x = hitPoint.x;
            float y = hitPoint.y + player.SelectedObject.transform.position.y;
            float z = hitPoint.z;
            destination = new Vector3(x, y, z);
            //Debug.Log("Start_Move");
            StartMove(destination);
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Are you using unity new UI with a button inside(and so a UI.Canvas), and a collider on the ground to detect the clicks? I think that if you use a box collider on the ground to detect "I pressed the ground" event with "OnMouseUp()" then you should have no problems \$\endgroup\$ Commented Dec 4, 2016 at 16:11
  • 1
    \$\begingroup\$ Check if you have Even System in your hierarchy. !EventSystem.current.IsPointerOverGameObject() should work just fine with it. Check out this video if it still doesn't work UI Block \$\endgroup\$ Commented Dec 4, 2016 at 16:32
  • 1
    \$\begingroup\$ 'Also, adding this as check didn't work: if (!EventSystem.current.IsPointerOverGameObject() )' I would like to see more context around this, like what exactly happened (just "didn't work" is pretty vague) and what was the code, because using this check is the answer to your problem. \$\endgroup\$
    – jhocking
    Commented Dec 5, 2016 at 19:29

1 Answer 1

1
\$\begingroup\$

It looks like you're using raycasting. Put a 3D object behind your interface, make it really small on Z coordinates and scale it to your interface on X and Y (or the coordinate system you are using). Make sure they are hidden behind your interface (make Z coorditane 1 or more).

You do need to use the Screen Space - Camera option with this, because Screen Space - Overlay won't work (as seen in the gif). That should stop any raycasting from going through.

Something like this: enter image description here

\$\endgroup\$

You must log in to answer this question.

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