Unity制作飞机大战第一步
1.把资源包导入Unity并搭建好场景
2.并把做好的预制体 拖入一个新的文件夹
如下图:
飞机控制脚本,此脚本挂在玩家飞机上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShipCtr : MonoBehaviour
{
public float MoveSpeed;
public float FireTime;
public GameObject PlayerBullet;
//私有变量同意使用_下划线
private Rigidbody _rig;
private AudioManagerCtr _audioManager;
private float _timer; // 计时器
private GameObject _obj;
private CreateGameManagerCtr _createGameManagerCtr;
void Start()
{
_obj = transform.Find("Protect").gameObject;
FireTime = 0.25f;
_rig = GetComponent<Rigidbody>();
if (null == _rig)
{
_rig = gameObject.AddComponent<Rigidbody>();
}
_rig.useGravity = false;
if (MoveSpeed == 0)
{
MoveSpeed = 5;
}
//找到标签的对象 并得到其组件
_audioManager = GameObject.FindWithTag("AudioManager").GetComponent<AudioManagerCtr>();
_createGameManagerCtr = GameObject.FindWithTag("CreateGameManager").GetComponent<CreateGameManagerCtr>();
_timer = 0;
}
//逻辑
void Update()
{
if(_createGameManagerCtr.PlayerIsProtected)
{
_obj.SetActive(true);
}else
{
_obj.SetActive(false);
}
_timer += Time.deltaTime;
if (Input.GetMouseButton(0) && _timer >= FireTime) //用于判断发射的时间间隔
{
//实例化子弹
_timer = 0;
GameObject obj = Instantiate(PlayerBullet);
obj.transform.position = gameObject.transform.