Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity check when clicked on object

 
void Update()
{
  // Check for mouse input
  if (Input.GetMouseButton(0))
  {
  	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   	RaycastHit hit;
   	// Casts the ray and get the first game object hit
   	Physics.Raycast(ray, out hit);
   	Debug.Log("This hit at " + hit.point );
  }
}
Comment

unity detect when an object has been clicked

public class MyObject : MonoBehaviour, IPointerDownHandler
{
    private void Start()
    {
        AddPhysics2DRaycaster();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
    }

    private void AddPhysics2DRaycaster()
    {
        Physics2DRaycaster physicsRaycaster = FindObjectOfType<Physics2DRaycaster>();
        if (physicsRaycaster == null)
        {
            Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to create directory folder in c# 
Csharp :: c# loop through array 
Csharp :: move gameobject in unity 2d 
Csharp :: unity look at 2d 
Csharp :: how to input a double in c# 
Csharp :: random value in array c# 
Csharp :: Schema::defultString larvel 
Csharp :: get random file in directory c# 
Csharp :: how to edit Camera.size property unity 
Csharp :: unity destroy object when out of screen 
Csharp :: remove a specific line in richtextbox c# 
Csharp :: Unity rotate player to mouse point slowly 
Csharp :: how to do a web request unity 
Csharp :: get the path of executable c# 
Csharp :: c# how to convert string to int 
Csharp :: message box in visual studio 
Csharp :: c# separate string by a new line 
Csharp :: xml node update attribute value c# 
Csharp :: get all devices in game unity 
Csharp :: unity vector3.distance giving nonsensical values 
Csharp :: get random from list c# 
Csharp :: how to do if comands in c# 
Csharp :: add leading zeroes in c# 
Csharp :: hello in c# 
Csharp :: how to generate random letters in C# 
Csharp :: unity agent does not move 
Csharp :: c# loading assembly at runtime 
Csharp :: unity unparent 
Csharp :: button action asp net 
Csharp :: round double c# 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =