当前位置:   article > 正文

【Unity3D】游戏常用点整理-飞机射击类游戏_transform spawnpos

transform spawnpos

以下内容为泰课视频的学习笔记


1)飞机控制

在有限的范围内上下左右移动

  1. public class Boundary {
  2. public float xMin = -6.5f;
  3. public float xMax = 6.5f;
  4. public float zMin = -1.5f;
  5. public float zMax = 11.0f;
  6. }

public Boundary boundary;

  1. void FixedUpdate() {
  2. float h = Input.GetAxis("Horizontal");
  3. float v = Input.GetAxis("Vertical");
  4. Vector3 move = new Vector3(h, 0f, v);
  5. rigidbody.velocity = speed * move;
  6. rigidbody.position = new Vector3(
  7. Mathf.Clamp(rigidbody.position.x,boundary.xMin, boundary.xMax),
  8. 0,
  9. Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
  10. );
  11. }

设定一个speed 


2)发射子弹

  1. public GameObject bolt;//子弹
  2. public Transform spawnpos;// 发射位置
  3. public float fireRate = 0.25f;//发射时间间隔
  4. private float nextFire;//下次发射时间
  5. public AudioClip fire;
  6. void Update() {
  7. if (Input.GetButton("Fire1")&&(Time.time>nextFire)) {
  8. nextFire = Time.time + fireRate;
  9. Instantiate(bolt, spawnpos.position, spawnpos.rotation);
  10. audio.PlayOneShot(fire);
  11. }
  12. }
加了生效,记得要添加一个空的Audio Source


3)生成敌人

public Vector3 spawnValues;

  1. void Enemy() {
  2. GameObject o = enemy[Random.Range(0, enemy.Length)];
  3. Vector3 p = new Vector3(Random.Range(-spawnValues.x,spawnValues.x),spawnValues.y,spawnValues.z);
  4. Quaternion q = Quaternion.identity;
  5. Instantiate(o, p, q);
  6. }

enemy数组装不同的敌人

在spawnValues附近生成


4)敌人移动

  1. public float speed = 10;
  2. void Start () {
  3. rigidbody.velocity = transform.forward * speed;
  4. }

5)几波敌人的进攻

  1. public int NumPerWave = 10;//一波敌人数量
  2. public float spawnWait = 1f;//敌人间隔出现时间
  3. public float startWait = 2f;//游戏开始等待时间
  4. public float waveWait = 4f;//每两波间隔出现时间
  5. void Start () {
  6. StartCoroutine(SpawnWaves());
  7. }
  8. IEnumerator SpawnWaves() {
  9. yield return new WaitForSeconds(startWait);
  10. while (true) {
  11. for (int i = 0; i < NumPerWave; i++) {
  12. Enemy();//生成敌人
  13. yield return new WaitForSeconds(spawnWait);
  14. }
  15. yield return new WaitForSeconds(waveWait);
  16. }
  17. }

6)kill与反kill

  1. public GameObject enemyExplosion;
  2. public GameObject playerExplosion;
  3. void OnTriggerEnter(Collider other) {
  4. if (other.gameObject.tag == "player")
  5. {
  6. Instantiate(playerExplosion, this.transform.position, this.transform.rotation);
  7. }
  8. Destroy(other.gameObject);
  9. Destroy(this.gameObject);
  10. Instantiate(enemyExplosion, this.transform.position, this.transform.rotation);
  11. }

对象是敌人,遇到子弹bang,遇到玩家bang,分别播放动画


下面一些小东东

7)移动

  1. public float speed = 10;
  2. void Start () {
  3. rigidbody.velocity = transform.forward * speed;
  4. }
8)边界设置(出边界,就销毁)

添加一gameobject,collider设置成边界大小或稍大点。

  1. public class DestroyByBoundary : MonoBehaviour {
  2. void OnTriggerExit(Collider other) {
  3. Destroy(other.gameObject);
  4. }
  5. }
9)让敌人滚动起来

  1. public class RandomRotater : MonoBehaviour {
  2. public float tumble = 5;
  3. void Start () {
  4. rigidbody.angularVelocity = Random.insideUnitSphere * tumble;
  5. }
  6. }

10) bang 几秒后,自动销毁对象

  1. public class DestroyByTime : MonoBehaviour {
  2. public float lifetime;
  3. void Start () {
  4. Destroy(this.gameObject, lifetime);
  5. }
  6. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/104154
推荐阅读
相关标签
  

闽ICP备14008679号