Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

use raycast unity new input system

With this code somewhere in your scene:

using UnityEngine.InputSystem;
using UnityEngine;

public class MouseClicks : MonoBehaviour
{
    [SerializeField]
    private Camera gameCamera; 
    private InputAction click;

    void Awake() 
    {
        click = new InputAction(binding: "<Mouse>/leftButton");
        click.performed += ctx => {
            RaycastHit hit; 
            Vector3 coor = Mouse.current.position.ReadValue();
            if (Physics.Raycast(gameCamera.ScreenPointToRay(coor), out hit)) 
            {
                hit.collider.GetComponent<IClickable>()?.OnClick();
            }
        };
        click.Enable();
    }
}
You can add an IClickable Interface to all GameObjects that want to respond to clicks:

public interface IClickable
{
    void OnClick();
}
and

using UnityEngine;

public class ClickableObject : MonoBehaviour, IClickable
{
    public void OnClick() 
    {
        Debug.Log("somebody clicked me");
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: an existing connection was forcibly closed by the remote host. .net core 
Csharp :: c# tell if list object is empty 
Csharp :: c# char 
Csharp :: c# windows forms open directory in explorer 
Csharp :: xamarin picker 
Csharp :: textblock line break 
Csharp :: How to find out if a file exists in C# / .NET? 
Csharp :: c# remove all whitespaces from string 
Csharp :: subtract days c# 
Csharp :: c# create array with n elements 
Csharp :: is number c# 
Csharp :: c# switch case greater than 
Csharp :: get unique array based on value in c# 
Csharp :: blazor ref to component in if 
Csharp :: entity framework insert 
Csharp :: c# how to crete array 
Csharp :: c# allowedusernamecharacters 
Csharp :: c# xml get root attributes 
Csharp :: unity convert number to notation 
Csharp :: c# remove the last character of a string 
Csharp :: datatable iqueryable c# linq 
Csharp :: c# system.text.json deserialize 
Csharp :: pause unity game 
Csharp :: get domain name from email in asp.net c# 
Csharp :: unity position ui element 
Csharp :: c# remove time in datetime 
Csharp :: slither io hack 
Csharp :: two linked list intersection 
Csharp :: C# xamaring form change text on label 
Csharp :: c# datagridview set column header alignment 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =