• 欢迎光临~

30 unity在3d地形上放置建筑

开发技术 开发技术 2022-10-24 次浏览

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

public class NewBehaviourScript : MonoBehaviour
{

public Terrain terrain;
public Camera maincamera;
public bool isclickbutton = false; //是否点击选择了一个预制体
public GameObject building; //预制体
public Vector3 placeposition;
// Start is called before the first frame update
void Start()
{
terrain.terrainData.size = new Vector3(20, 0, 20);
Vector3 mousePosition = Input.mousePosition; //获取鼠标所在坐标。

}

// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
print("鼠标左键点击");
Raycastterrain();

GameObject enemys = Instantiate(building, placeposition, Quaternion.identity);


}


if (Input.GetMouseButtonDown(1))

print("鼠标右键点击");

}

private void Raycastterrain() //确定放置位置。
{
RaycastHit hit;
Ray ray = maincamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
if(hit.transform.name=="Terrain")
placeposition= hit.point;

}
}
}

程序员灯塔
转载请注明原文链接:30 unity在3d地形上放置建筑
喜欢 (0)