Unityのコンストラクションレイ

実装された機能

カメラからマウスの位置までの光線

レイはオブジェクトと衝突し、衝突したオブジェクトのスクリプトを返し、スクリプトの状態を取り出します。

左ボタンを押す ------ 状態に応じて別のウィンドウに戻ります

判断とは何ですか?空き地 0 / 購入済み 1 / 建築済み 2

レイの生成、レイによって返されるオブジェクト情報

左ボタンを押す ------ 状態に応じて別のウィンドウに戻ります

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

public class GameControler : MonoBehaviour
    
{
    GroundProperties groundPro;//用来存放射线碰撞的对象挂载的脚本信息信息
    void Start()
    {
        
    }

    void Update()
    {   
        //建造射线
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray,out hit))
        {
            groundPro = hit.collider.GetComponent<GroundProperties>();
            //返回射线碰撞到对象挂载的脚本信息
        }
        //左键按下
        //对象不能为空
        //根据碰撞对象来显示状态
        if(Input.GetMouseButtonDown(0))
        {
            if (groundPro != null)
            {
                if (groundPro.State == 0)
                {
                    //显示购买窗口
                    print("0");
                }
                else if (groundPro.State == 1)
                {
                    //显示建造窗口
                    print("1");
                }
                else if (groundPro.State == 2)
                {
                    //显示信息窗口
                    print("2");
                }
            }
        }
    }
}

オブジェクトの状態

変数を使用して状態を保存する

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

public class GroundProperties : MonoBehaviour

{
    //存储土地的状态
    //空地0    已购买1  已建造2

    public int State=0;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Guess you like

Origin blog.csdn.net/weixin_70271498/article/details/127794466