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;
}
}
}