www.sillitoy.com
www.sillitoy.com

Reputation: 35

Unity3d: how to detect click within an area

In a Unity3d application am trying to detect a click in a certain squared area of the current camera. Is there any way to do that?

Thank you

Upvotes: 2

Views: 14401

Answers (2)

Haluk Özgen
Haluk Özgen

Reputation: 31

I also use this code piece to detect if the cursor in Game View.

Vector2 mPos = Mouse.current.position.ReadValue();
if (mPos.x > 0 && mPos.x < Screen.width && mPos.y > 0 && mPos.y < 
Screen.height)
{
    DoSomeMethods();
}

But i feel something is still not like i want. :)

Upvotes: 0

Lance
Lance

Reputation: 5800

Is this not what you're looking for?

http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

** EDIT **

using UnityEngine;

public class example : MonoBehaviour
{
    void Update()
    {
        // Left-half of the screen.
        Rect bounds = new Rect(0, 0, Screen.width/2, Screen.height);
        if (Input.GetMouseButtonDown(0) && bounds.Contains(Input.mousePosition))
        {
            Debug.Log("Left!");            
        }
    }
}

Upvotes: 8

Related Questions