Demon_Tank (坦克移动发射子弹)
using UnityEngine; using System.Collections; public class Tank : MonoBehaviour { //子弹预设体 public GameObject bullet; //发射点 private Transform firePoint; //移动速度 public float moveSpeed = 3f; //转身速度 public float turnSpeed = 3f; //横纵轴 float hor; float ver; void Start() { //获取发射点 firePoint = transform.Find ("Top/Gun/FirePoint"); } void Update() { //获取键盘纵轴值 ver = Input.GetAxis ("Vertical"); //坦克前后移动 transform.position += transform.right * ver * Time.deltaTime * moveSpeed; //获取键盘横轴值 hor = Input.GetAxis ("Horizontal"); //坦克转身 transform.eulerAngles += hor * Vector3.up * turnSpeed; //如果按下空格发射子弹 if (Input.GetKeyDown (KeyCode.Space)) { Fire (); } } /// <summary> /// 子弹发射 /// </summary> void Fire() { //生成子弹 GameObject currentBullet = (GameObject)Instantiate (bullet, firePoint.position, Quaternion.identity); //给子弹一个飞行方向 currentBullet.GetComponent<Bullet> ().dir = transform.right; } }