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()
    {
        
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_70271498/article/details/127794466