此脚本挂在相机MainCamera上
public class Shoot : MonoBehaviour {
public GameObject bullet;
// Use this for initialization
void Start () {
//GameObject.Instantiate(bullet,transform.position,transform.rotation);
//此时clone的小球的位置变成与相机位置一致
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//鼠标点击的位置
print(hit.point);
GameObject.Instantiate(bullet, hit.transform);
}
}
}
}
在指定位置上clone物体:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour {
public GameObject bullet;
// Use this for initialization
void Start () {
//GameObject.Instantiate(bullet,transform.position,transform.rotation);
//此时clone的小球的位置变成与相机位置一致
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
GameObject.Instantiate(bullet, transform.position, transform.rotation);
}
}
}