1.用了物理射线的点获取
2.用了Instantiate实例化
3.和创建墙一样需要一个预制体和空物体,脚本依附于空物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Zidan : MonoBehaviour
{
public GameObject m_zidan; //定义公共游戏对象m_zidan
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //强制转化为UnityEngine里的类,可能其它包里包含了这个类(我这里用Ray ray定义对象会报错,所以加入UnityEngine)
RaycastHit hit; //定义一个用于记录碰撞信息结构体的对象
Physics.Raycast(ray, out hit); //物理类下射线检查方法,如果发生物理碰撞,返回值为真,并将碰撞信息存储到hit中
if (Input.GetMouseButtonDown(0)) //按下鼠标左键
{
if (hit.collider != null) //如果碰撞信息中的碰撞体不为空
{
Vector3 start = Camera.main.gameObject.GetComponent<Transform>().position; //定义三