[Unity]ビジュアルスクリプト – クリックしたオブジェクトを取得する

マウスクリックしたオブジェクトを取得するスクリプトをUnity Visual Scripting(ビジュアルスクリプティング)をつかって実装してみます。
これからの
ビジュアルスクプリティングの場合
通常のC#スクリプトは以下のようになります。C#スクリプトを参考に、ビジュアルスクリプティングを作成すると、簡単にビジュアルスクリプトを作成することができます。C#スクリプトよりも簡潔で分かりやすいコードになると思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class GetMouseClicked : MonoBehaviour | |
{ | |
GameObject clickedGoj; | |
// Update is called once per frame | |
void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit = new RaycastHit(); | |
if (Physics.Raycast(ray, out hit)) | |
{ | |
clickedGoj = hit.collider.gameObject; | |
Debug.Log(clickedGoj.name); | |
} | |
} | |
} | |
} |