(Unity)照準器で狙ったオブジェクトのタグとマテリアルを変換する(Raycastの活用)

(スクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ListBlock_Aim : MonoBehaviour
{
    public GameObject aim;
    public Material redBlock;
    public AudioClip sound;

    void Start()
    {
        Cursor.visible = false;
    }

    void Update()
    {
        aim.transform.position = Input.mousePosition;

        if(Input.GetKeyDown(KeyCode.Space))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if(Physics.Raycast(ray, out hit))
            {
                GameObject target = hit.collider.gameObject;

                if(target.tag == "Block")
                {
                    // タグの変換
                    target.tag = "Lock";

                    // マテリアルの変換
                    target.GetComponent<MeshRenderer>().material = redBlock;

                    AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
                }
            }
        }
    }
}

(実行結果)