上一个脚本那个网格就当个辅助工具吧,拿来看点用的
这是一个测试用脚本,检测该基本功能的实现
这个脚本可以在点击到的网格块顶点生成预制件,初步实现了网格建造功能
内容比较简单,上代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///-----------------------------------------------------------------------------------------
/// Founction: Build the cylinder on grid by clicking
/// InvolvingKnowledge: Ray
/// Author: AttouchAusturo
/// LatestReviseTime: 2021.12.18
///-----------------------------------------------------------------------------------------
public class GetGroundAreaByClicking : MonoBehaviour
{
//射线获取坐标
private Ray ray;
private RaycastHit hit;
private Vector3 target;//鼠标点击位置的世界坐标
//坐标转换
private Vector3 groundArea;
//要建造的预制件
public GameObject cylinderPrefab;
public GameObject buildingFounctionTest;
private void Update()
{
//当点击鼠标左键时
if (Input.GetMouseButtonDown(0))
{
//鼠标在屏幕的位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
//绘制出一条从相机射出的红色射线
Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
}
target = hit.point;//获取点击位置的世界坐标
groundArea = new Vector3((int)target.x / 10 * 10, 0, (int)target.z / 10 * 10);//坐标取整
buildingFounctionTest = Instantiate(cylinderPrefab, groundArea, Quaternion.identity) as GameObject;//在格子顶点生成该物体
}
}
}
文章评论