I am trying to check if a GameObject is clicking another GameObject.
I am trying to make a 2D shooter like Duck Hunt so this is important.
I have tried IPointerDownHandler but found out that it is no longer supported.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using UnityEngine.EventSystems;
public class ShootScript : MonoBehaviour//, IPointerDownHandler
{
private Vector3 mousePosition;
void Start()
{
gameObject.SetActive(true);
}
void Update()
{
/*mousePosition = Input.mousePosition;
if (mousePosition.x == transform.position.x && mousePosition.y == transform.position.y && Input.GetMouseButtonDown(0))
{
gameObject.SetActive(false);
}*/
}
/*public void OnPointerDown(PointerEventData pointerEventData)
{
gameObject.SetActive(false);
}*/
}
I have a GameObject "Shooter" and a GameObject "Enemy". I want to check if the shooter is clicking the enemy.
Unity version: 2019.4.40f1
Thanks!
IPointerDownHandleris used, if you are clicking on something. But what you are describing, you want to detect collision docs.unity3d.com/ScriptReference/… or a raycast if your bullets are instant. One GameObject is never clicking on a second one \$\endgroup\$gameObject.SetActive(true);doesn't make sense to put inStart()because that function only gets called if the game object is already active. Your commented-out code inUpdate()is comparing the mouse position in screen space to a game object position in world space: these are not measured on the same grid. \$\endgroup\$