飞盘射击
一.功能需求.... 1
1.UI 展示.... 2
2.主角控制.... 2
3.AI 控制.... 2
4.音效特效.... 2
二. UI 展示.... 1
2.1 StartUI 1
2.2 GameUI 2
2.3 EndUI 2
三.主角的控制.... 3
3.1 光线由枪口发出在枪口添加一个空物体用于获取其坐标点(start) 3
3.2 枪指着鼠标的位子,枪绕着手臂旋转,需在手臂上添加一个空物体。.... 4
3.3 鼠标左键实现消灭目标.... 4
四.AI的控制.... 7
4.1 飞盘在空中随机位置生成。.... 7
4.2 当枪的射线碰撞到飞盘后,飞盘破碎掉落,分数加 1 分。.... 8
五.音效控制.... 8
六 效果展示.... 9
一.功能需求
1.UI 展示
游戏开始 UI:游戏名称,游戏玩法,开始按钮。
游戏进行 UI:显示时间倒计时,显示当前分数。
游戏结束 UI:分数,返回按钮。
2.主角控制
持枪的一个手臂,枪口的朝向一直是朝向鼠标在屏幕上的位置。
按住鼠标左键发射干掉目标。
3.AI 控制
飞盘在空中随机位置生成。
当枪的射线碰撞到飞盘后,飞盘破碎掉落,分数加 1 分。
4.音效特效
游戏背景音乐,射击声。
二. UI 展示
2.1 StartUI
2.2 GameUI
2.3 EndUI
三.主角的控制
3.1 光线由枪口发出在枪口添加一个空物体用于获取其坐标点(start)
3.2 枪指着鼠标的位子,枪绕着手臂旋转,需在手臂上添加一个空物体。
3.3 鼠标左键实现消灭目标
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weapon : MonoBehaviour {
private Transform tf;
private Transform t;
private LineRenderer l;
private AudioSource au;
private AudioSource back;
private GUIText sc;
private GUIText time;
private bool canmove =false;
private AudioSource m_bkaudio;
private float m_time = 20.0f;
public GUIText ui_time;
public int m_score = 0;
public GUIText ui_text;
private GameManage m_mg;
public int m_totleScore;
// Use this for initialization
void Start () {
tf = gameObject.GetComponent<Transform>();//获取当前挂载物体的transform组件
t = GameObject.Find("枪口").GetComponent<Transform>();
l = GameObject.Find("line").GetComponent<LineRenderer>();
au = GameObject.Find("Main Camera").GetComponent<AudioSource>();
back = GameObject.Find("audio").GetComponent<AudioSource>();
m_bkaudio = Camera.main.gameObject.GetComponent<AudioSource>();
m_bkaudio.enabled = false;
// ui_time = GameObject.Find("倒计时").GetComponent<GUIText>();
//ui_text = GameObject.Find("score").GetComponent<GUIText>();
m_mg = GameObject.Find("UI").GetComponent<GameManage>();
}
// Update is called once per frame
void Update () {
if (canmove){
ui_time = GameObject.Find("倒计时").GetComponent<GUIText>();
ui_text = GameObject.Find("score").GetComponent<GUIText>();
m_time-=Time.deltaTime;
ui_time.text="倒计时"+ (int)m_time +"秒";
if(m_time<0)
{
m_time = 20.0f;
m_mg.showEndUI();
}
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
tf.LookAt(hit.point);
Vector3 end = hit.point;
Vector3 start = t.position;
l.SetPosition(0, start);
l.SetPosition(1, end);
if (Input.GetMouseButtonDown(0)) //鼠标左键消灭物体
{
back.Play();
if (hit.collider.tag == "feipan")
{
m_score++;
ui_text.text="得分"+m_score+"分";
au.Play();
Transform transform=hit.collider.gameObject.GetComponent<Transform>();
Transform[] rb = transform.parent.GetComponentsInChildren<Transform>();
for (int i = 1; i < rb.Length; i++)
{
rb[i].gameObject.AddComponent<Rigidbody>();
}
//获取父物体下所有子物体为所有子物体添加钢体组件实现对靶子的消灭,i=0时指的是父物体,所以需要i=1开始
}
}
}
}
public void setcanGame(bool cangame){ //决定是否开始游戏的方法。
canmove=cangame; //移动手臂
m_bkaudio.enabled = cangame;//游戏音乐
l.enabled = cangame;//射线渲染
}
public void setscore(int score)//设置分数
{
m_score = score;
}
public void getsocre()//获取分数
{
print(m_score);
}
}
四.AI的控制
4.1 飞盘在空中随机位置生成。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class panzi : MonoBehaviour {
// Use this for initialization
public GameObject pz;
private float time = 2.0f;
private bool Cancreate = false;
void Start () {
}
// Update is called once per frame
void Update()
{
if (Cancreate)
{
time = time - Time.deltaTime;
if (time < 0)
{
CreateFeipan();
time = 2.0f;
}
}
}
private void CreateFeipan(){
for (int i=0;i<5;i++){
Vector3 pos =new Vector3(Random.Range(-6.0f,5.0f),Random.Range(1.0f,5.0f),Random.Range(1.0f,6.0f)); //定义盘子随机生成的位子范围
GameObject obj= Instantiate(pz, pos, Quaternion.identity);
Destroy(obj, 3);
}
}
public void cancreate(bool createfeipan) //是否产生盘子
{
Cancreate = createfeipan;
}
}
4.2 当枪的射线碰撞到飞盘后,飞盘破碎掉落,分数加 1 分。
if (Input.GetMouseButtonDown(0)) //鼠标左键消灭物体
{
back.Play();
if (hit.collider.tag == "feipan")
{
m_score++; //分数加一
ui_text.text="得分"+m_score+"分";
au.Play();
Transform transform=hit.collider.gameObject.GetComponent<Transform>();
Transform[] rb = transform.parent.GetComponentsInChildren<Transform>();
for (int i = 1; i < rb.Length; i++)
{
rb[i].gameObject.AddComponent<Rigidbody>();
}
//获取父物体下所有子物体为所有子物体添加钢体组件实现对靶子的消灭,i=0时指的是父物体,所以需要i=1开始
}
}
五.音效控制
if (Input.GetMouseButtonDown(0)) //鼠标左键消灭物体
{
back.Play();//背景音乐
if (hit.collider.tag == "feipan")
{
m_score++; //分数加一
ui_text.text="得分"+m_score+"分";
au.Play();//射击音乐
Transform transform=hit.collider.gameObject.GetComponent<Transform>();
Transform[] rb = transform.parent.GetComponentsInChildren<Transform>();
for (int i = 1; i < rb.Length; i++)
{
rb[i].gameObject.AddComponent<Rigidbody>();
}
//获取父物体下所有子物体为所有子物体添加钢体组件实现对靶子的消灭,i=0时指的是父物体,所以需要i=1开始
}
}
六 效果展示