一、效果图
大概的效果图如下,实现的功能:😁通过点击鼠标飞机会射出子弹,若子弹超出屏幕外会自动销毁。
![在这里插入图片描述](https://img-blog.csdnimg.cn/28928ce87b184511b845e00c9110d5a2.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAWnpf56iL5bqP5aqb,size_13,color_FFFFFF,t_70,g_se,x_16#pic_center)
二、功能实现
1、场景对象
![在这里插入图片描述](https://img-blog.csdnimg.cn/692ec97a7694427a977c03c6d85341c3.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAWnpf56iL5bqP5aqb,size_6,color_FFFFFF,t_70,g_se,x_16#pic_center)
- 【Main Camera】 代表主摄像机,需要这个我们才能看到如今这个视角
- 【游戏主控】 上面不挂载任何图片对象,但挂了一个c#文件,主要用来全局设置。比如这个项目所涉及到的时间针率。
![在这里插入图片描述](https://img-blog.csdnimg.cn/f097184621b3476cb5c2af64c3dc8b11.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAWnpf56iL5bqP5aqb,size_10,color_FFFFFF,t_70,g_se,x_16#pic_center)
3.【子弹】 这个子弹可惜忽略,主要是通过它来创建子弹预制体
4.【子弹Prefab】此对象需要挂MyBullet,特别提醒需要在【飞机】脚本下挂它 ![在这里插入图片描述](https://img-blog.csdnimg.cn/c60912ac2f404bf2856f64210347f50b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAWnpf56iL5bqP5aqb,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
5.【飞机】 挂MyJet ![在这里插入图片描述](https://img-blog.csdnimg.cn/2d3170beb8414e0caa8aa75dd4cbbfee.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAWnpf56iL5bqP5aqb,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
2、代码实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyGame : MonoBehaviour
{
void Start()
{
Application.targetFrameRate= 60;
}
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyBullet : MonoBehaviour
{
public float speed = 5.5f;
void Start()
{
}
void Update()
{
float dy = speed * Time.deltaTime;
transform.Translate(0, dy, 0, Space.Self);
Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
if (sp.y > Screen.height)
{
Destroy(this.gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyJet : MonoBehaviour
{
public GameObject myPrefab;
private float interval = 0.4f;
private float count = 0;
void Start()
{
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Fire();
}
}
private void Fire()
{
GameObject bullet = Instantiate(myPrefab);
bullet.transform.position = transform.position + new Vector3(0, 1f, 0);
}
}
三、总结
现在我只是刚开始,给自己打气遇到挫折难题不要放弃,踏踏实实学习,一定会在毕业前修炼成大师的! 对的,这个游戏还没结束,下一章会继续写键盘控制飞机、子弹击中敌人,敌人消失。😊😊😊😊下一章见!
|