下载文件:Shooter
using System.Collections; using System.Collections.Generic; using UnityEngine; /** * 这个组件可以让一个对象可以射击出另外一个对象。 * 需要指定发射的物体 Bullte 和发射点 ShootPoint。 * 作者:weivain@qq.com www.weiva.com */ public class Shooter : MonoBehaviour { public GameObject bullet; //申明子弹原型 public Transform shootPoint;//子弹被克隆时的初始位置 //对象被创建时执行 void Start () { } //每一帧被执行 void Update () { } //每一物理帧被执行 void FixedUpdate(){ //如果“开火1”按钮被按下时 if (Input.GetButtonDown ("Fire1")) { //克隆一个对象,并且用 newBullet 来代表它。 GameObject newBullet = GameObject.Instantiate (bullet); //把克隆出来的对象放到射击点的位置 newBullet.transform.position = shootPoint.position; //把克隆出来的对象旋转角度与射击点一致 newBullet.transform.rotation = shootPoint.rotation; //设置克隆出来的对象速度为:子弹的前方 x 30 个单位(既每秒30米) newBullet.GetComponent<Rigidbody> ().velocity = newBullet.transform.forward * 30; } } }