1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class TankAttack : MonoBehaviour
6 {
7 //创建一个物体
8
9 //(发射3D发射子弹)
10
11 //获取需要发射的物体
12
13 public GameObject shell;
14
15 //获取键盘上的某个按键 空格键
16
17 public KeyCode firekey = KeyCode.Space;
18
19 //物体发射的位置
20
21 public Transform firePos;
22
23 //物体发射的速度
24 public float shellSpeed = 30f;
25
26 // Use this for initialization
27 void Start()
28 {
29 //在Start函数中
30
31 //获取物体发射的位置
32
33
34 firePos = transform.Find("FirePosition");
35 }
36 void Update()
37 {
38 //在Updata函数中
39
40 //判断是否为空格键
41
42 if (Input.GetKeyDown(firekey))
43 {
44 //在firePos处创建物体
45 GameObject obj = GameObject.Instantiate(shell, firePos.position, firePos.rotation) as GameObject;
46 //给物体一个速度
47
48 obj.AddComponent<Rigidbody>().velocity = obj.transform.forward * shellSpeed;
49 }
50
51
52 }
53 }