using UnityEngine;
using System.Collections;
public class TankWeapons : MonoBehaviour {
public GameObject shell;
public float shootPower;//确定子弹的发射力
public Transform shootPoint;//确定子弹的发射点。新建一个tank的空子物体,用来记录子弹的发射位置
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {//当按下空格时,调用Shoot()函数,所以用GetKeyDown
Shoot();
}
}
void Shoot(){
GameObject newShell = Instantiate (shell, shootPoint.position, shootPoint.rotation) as GameObject;//用Instantiate函数克隆一个Object,并将Object转化为GameObject,存于newShell
Rigidbody r = newShell.GetComponent<Rigidbody> ();//r定义为newShell的刚体
r.velocity = shootPoint.forward * shootPower;//定义r的速度的方向及大小,速度方向为shootPoint.forward,大小为shootPower
}
}
注: