好友
阅读权限10
听众
最后登录1970-1-1
|
浯茗仔仔
发表于 2018-11-26 11:48
本帖最后由 浯茗仔仔 于 2018-11-26 16:52 编辑
unity打飞机(一) 飞机的移动动画化切换,子弹的生成,超级子弹的生成和炸弹的数量,飞机音效
刚学了一个星期的Unity,作业是制作打飞机游戏,写完当然要保存啦,所以一是保存下学习记录,二当然是骗点分(实诚吧)
[C#] 纯文本查看 复制代码 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HeroMove : MonoBehaviour {
//判断鼠标是否点击
public bool isMouseDown = false;
//当鼠标点击时获取当前坐标
private Vector3 lastMousePosition = Vector3.zero;
//飞机飞行图片切换动画
private SpriteRenderer render;
//飞机切换图画数组
public Sprite[] heroSprite;
//飞机切换图画数组
public Sprite[] overHeroSprite;
//飞机切换动画的时间
private float time;
//子弹生成位置
public Transform bulletLocation;
//子弹预设物
public Transform bulletModel;
//子弹生成时间
private float bulletRate = 0.2f;
private float bulletTime = 0;
//超级子弹时间
private int superBulletNum = 0;
//超级子弹生成预设物
public Transform superBullet;
//炸弹数量
private int bombNum = 0;
//音频控件
private AudioSource audioSource;
//子弹音频播放
public AudioClip bulletClip;
//得到子弹音频播放
public AudioClip getBulletClip;
//炸弹音频播放
public AudioClip bombClip;
//得到炸弹音频播放
public AudioClip getBombClip;
//游戏结束音频播放
public AudioClip overClip;
private bool over = false;
// Use this for initialization
void Start () {
render = GetComponent<SpriteRenderer> ();
audioSource = GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
//飞机的飞行图片切换
time += Time.deltaTime;
int heroTime = (int)(time / (1f / 10));
int renderInedx = heroTime % 2;
if (over) {
if (renderInedx < overHeroSprite.Length) {
render.sprite = overHeroSprite [renderInedx];
}else {
Destroy (gameObject);
over = false;
}
} else {
if (renderInedx < heroSprite.Length) {
render.sprite = heroSprite [renderInedx];
}
}
//当鼠标左键按下时
if (Input.GetMouseButtonDown(0)) {
isMouseDown = true;
}else if (Input.GetMouseButtonUp(0)) {
//当鼠标左键抬起时
isMouseDown = false;
lastMousePosition = Vector3.zero;
}
//当鼠标左键按下并且按下时间超过0f
if (isMouseDown && Time.timeScale > 0f) {
if (lastMousePosition != Vector3.zero) {
//获取鼠标点击坐标和当前飞机左边的偏移量
Vector3 offSet = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;
//让飞机的坐标加上偏移量使移动
transform.position += offSet;
//限制坐标使飞机不能越界
CheckPosition();
}
//屏幕坐标转游戏实际坐标
lastMousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
}
//创建子弹
bulletTime += Time.deltaTime;
if (bulletTime > bulletRate) {
if (superBulletNum > 0) {
superBulletNum--;
}
CareBullet ();
bulletTime = 0;
//Time.timeScale = 0;
}
}
//限制飞机范围
void CheckPosition(){
Vector3 heroPos = transform.position;
// x +-2.8 y +-4.5
heroPos.x = Mathf.Clamp(heroPos.x, -2.8f, 2.8f);
heroPos.y = Mathf.Clamp(heroPos.y, -4.5f, 4.5f);
transform.position = new Vector3 (heroPos.x, heroPos.y, 0f);
}
//生成子弹
void CareBullet(){
//子弹音效播放
audioSource.clip = bulletClip;
audioSource.Play();
if (superBulletNum > 0) {
//生成预设物
var bu = Instantiate (superBullet);
//设定预设物的坐标
//bu.position = new Vector3(bulletLocation.position.x - 1.5f, bulletLocation.position.y - 1, 0f);
bu.position = bulletLocation.position;
} else {
//生成预设物
var bu = Instantiate (bulletModel);
//设定预设物的坐标
bu.position = bulletLocation.position;
}
}
//触发器
void OnTriggerEnter2D(Collider2D other){
if (other.GetComponent<EnemyAndSuperMove> ().type == DownType.SuperBullet) {
//得到超级子弹音效播放
audioSource.clip = getBulletClip;
audioSource.Play ();
superBulletNum = 50;
} else if(other.GetComponent<EnemyAndSuperMove> ().type == DownType.Bomb){
//得到炸弹音效播放
audioSource.clip = getBombClip;
audioSource.Play ();
bombNum++;
ButtonAndText.bombNum = bombNum;
} else if (other.tag == "Enemy") {
//游戏结束音效播放
audioSource.clip = overClip;
audioSource.Play ();
over = true;
ButtonAndText.isOver = true;
}
}
//点击炸弹
public void OnClickBomb(){
if (bombNum > 0) {
audioSource.clip = bombClip;
audioSource.Play ();
bombNum--;
ButtonAndText.bombNum = bombNum;
GameObject[] enemyGO = GameObject.FindGameObjectsWithTag ("Enemy");
for (int i = 0; i < enemyGO.Length; i++) {
GameObject obj = enemyGO [i];
obj.GetComponent<EnemyAndSuperMove> ().EnemyBomb ();
}
}
}
}
还有二三四五六发到下一个帖子了,请移步 https://www.52pojie.cn/thread-828894-1-1.html
|
-
-
UI.rar
121.75 KB, 下载次数: 3, 下载积分: 吾爱币 -1 CB
ui图标
-
-
Audio.rar
163.71 KB, 下载次数: 2, 下载积分: 吾爱币 -1 CB
音效文件
-
-
Textures1.rar
571.97 KB, 下载次数: 4, 下载积分: 吾爱币 -1 CB
模型1
-
-
Textures2.rar
549.75 KB, 下载次数: 2, 下载积分: 吾爱币 -1 CB
模型2
免费评分
-
参与人数 2 | 吾爱币 +3 |
热心值 +2 |
收起
理由
|
苏紫方璇
| + 2 |
+ 1 |
欢迎分析讨论交流,吾爱破解论坛有你更精彩! |
Pear
| + 1 |
+ 1 |
谢谢@Thanks! |
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|