My current code allows for all objects in the scene to be clickable. How do I make it so only gameobjects with a tag be clickable?
public class InputManager : MonoBehaviour
{
private bool draggingItem = false;
private bool mouseDragging = false;
private GameObject draggedObject;
private Vector2 touchOffset;
void Update()
{
if (LeftClick)
{
mouseDragging = true;
}
if (mouseDragging)
{
DragOrPickUp();
}
if (RightClick) {
mouseDragging = false;
DropItem();
}
}
Vector2 CurrentTouchPosition
{
get
{
Vector2 inputPos;
inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
return inputPos;
}
}
private void DragOrPickUp()
{
var inputPosition = CurrentTouchPosition;
if (draggingItem)
{
draggedObject.transform.position = inputPosition + touchOffset;
}
else
{
RaycastHit2D[] touches = Physics2D.RaycastAll(inputPosition, inputPosition, 0.5f);
if (touches.Length > 0)
{
var hit = touches[0];
if (hit.transform != null)
{
draggingItem = true;
draggedObject = hit.transform.gameObject;
touchOffset = (Vector2)hit.transform.position - inputPosition;
}
}
}
}
private bool LeftClick
{
get
{
// returns true if either the mouse button is down or at least one touch is felt on the screen
return Input.GetMouseButton(0);
}
}
private bool RightClick
{
get
{
// returns true if either the mouse button is down or at least one touch is felt on the screen
return Input.GetMouseButton(1);
}
}
void DropItem()
{
draggingItem = false;
}
}