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 redirect to another page in button clicked in asp.net c# index.cshtml 
Csharp :: ??= mean C# 
Csharp :: c# unit test for throwing exception method 
Csharp :: add qtwidgets to cmake file 
Csharp :: serilog .net 6 
Csharp :: searching for keys in the registry 
Csharp :: how to use double in c# 
Csharp :: select distinct two columns entity framework c# 
Csharp :: c# import class from another file 
Csharp :: sealed method in c# 
Csharp :: c# check if object is of any generic type 
Csharp :: c# while loop 
Csharp :: c# validate xml 
Csharp :: c# .equals vs == 
Csharp :: linq map array 
Csharp :: c# sharepoint get users from column 
Csharp :: unitry raycast 
Csharp :: monogame print 
Csharp :: c# array of objects 
Csharp :: store data between razor pages 
Csharp :: #dictionery in c 
Csharp :: unity subtract class 
Csharp :: how to download somthing from one drive unity 
Csharp :: unity stop velocity movement 
Csharp :: wpf binding ancestor codebehind 
Csharp :: browser folder in wpf 
Csharp :: asp.net listbox disable selection 
Csharp :: ##[error]Dotnet command failed with non-zero exit code on the following projects 
Csharp :: c# copy all files in directory and subdirectories 
Csharp :: long string c# 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =