Unity实现弹球打砖块游戏

 更新时间:2022年05月11日 14:28:16   作者:璐希法  
这篇文章主要为大家详细介绍了Unity实现弹球打砖块游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例为大家分享了Unity实现弹球打砖块游戏的具体代码,供大家参考,具体内容如下

创作界面记录

摄像机

所需脚本

1射线shexian

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sheixian : MonoBehaviour {
? ? public GameObject blit;//定义一个公共的游戏物体来当子弹
? ? AudioSource au;//定义一个au音效
? ? // Use this for initialization
? ? void Start () {
? ? ? ? au = GetComponent<AudioSource>();//赋值音效
? ? }

? ? // Update is called once per frame
? ? void Update () {
? ? ? ? Ray ray;
? ? ? ? RaycastHit hit;
? ? ? ? //1.创建射线
? ? ? ? //2.射线检测并反馈结果
? ? ? ? //鼠标左键点击一个东西,然后反馈给我们物体信息
? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? {
? ? ? ? ? ? //把摄像机屏幕点转化为线 ? ? ? ? ? ? 获取鼠标坐标
? ? ? ? ? ? ray = Camera.main.ScreenPointToRay(Input.mousePosition);
? ? ? ? ? ? //创建射线
? ? ? ? ? ? if (Physics.Raycast(ray, out hit))//第一个参数是射线,第二个是碰撞的物体
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定义一个bt承接实例化的子弹, 对象实例化(实例化对象,位置[摄像机当前位置],不旋转)
? ? ? ? ? ? ? ? GameObject bt = GameObject.Instantiate(blit, transform.position, Quaternion.identity);
? ? ? ? ? ? ? ? au.Play();//播放音效
? ? ? ? ? ? ? ? //给一个方向 ? 点击位置的坐标-当前位置=一个向量;
? ? ? ? ? ? ? ? Vector3 dis = hit.point - transform.position;
? ? ? ? ? ? ? ? //给bt一个力
? ? ? ? ? ? ? ? bt.GetComponent<Rigidbody>().AddForce(dis * 300);
? ? ? ? ? ? }
? ? ? ? }
? ? }

}

把Sphere预制体拉入Blit框,添加Audio Source组件,AudioClip拉入子弹音效;取消Play On Awake

地板

给地板添加音乐来当背景音乐,再给个材质改变颜色

空物体copy

用来实例化砖块,挂载copysp脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class copysp : MonoBehaviour {
? ? public GameObject ga;//定义一个游戏物体


? ? // Use this for initialization
? ? void Copy () { ? ?//实例化预制体

? ? ? ? ? ? for (int i = 0; i < 25; i++)//用for循环来实现多个实例化
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定义一个随机向量 ? ? ? ? ?X坐标 ? ? ? ? ? ? ? ? ? ? ,Y坐标, ?Z坐标
? ? ? ? ? ? ? ? Vector3 ve3 = new Vector3(Random.Range(-5.0f, 5.0f), 10.0f, Random.Range(-5.0f, 5.0f));
? ? ? ? ? ? ? ? //实例化 ? ? 实例化物体,位置,是否旋转(不旋转)
? ? ? ? ? ? ? ? Instantiate(ga, ve3, Quaternion.identity);
? ? ? ? ? ? } ? ? ??
? ? }

? ? void Start()
? ? {
? ? ? ? //延时多次调用 ? (调用的方法,延时几秒,隔几秒再次调用)
? ? ? ? InvokeRepeating("Copy", 2, 6);
? ? }
? ? // Update is called once per frame
? ? void Update () {

? ? }
}

预制体Cube

给Box Collider一个反弹材质
***Unity物体碰撞时的反弹系数:也即Physic Material的 Bounciness属性。  

一句话,给物体的Collider添加Material属性即可

1、首先,物体要有Collider(BoxCollider, SphereCollider,PolygonCollider等)

2、创建一个Physic Material

Asset -> Create->Physic Material

看到Bounciness这个属性,区间是0到1,可以小数,其他暂不动。
0值:没有弹力,1值:没有能量损失的反弹。  

3、赋值给Collider的Material属性。

系统自带这几种物理材质

Bouncy:弹性材质。Ice:冰材质。Metal:金属材质。Rubber:橡胶材质。Wood:木头材质。*

如图

给一个Random Color脚本

using UnityEngine;
using System.Collections;

public class RandomColor : MonoBehaviour
{

? ? // Use this for initialization
? ? void Start()
? ? {
? ? ? ? //获取组件 ? ? ?材质.颜色
? ? ? ? this.gameObject.GetComponent<MeshRenderer>().material.color = RandomColor1();//给游戏物体添加下方随机颜色方法
? ? }
? ? /*float timer;
? ? //随着时间变换颜色
? ? // Update is called once per frame
? ? void Update()
? ? {
? ? ? ? timer -= Time.deltaTime;
? ? ? ? if (timer <= 0)
? ? ? ? {
? ? ? ? ? ? this.gameObject.GetComponent<MeshRenderer>().material.color = RandomColor1();
? ? ? ? ? ? timer = 1;

? ? ? ? }

? ? }*/

? ? public Color RandomColor1()
? ? {
? ? ? ? float r = Random.Range(0f, 1f);
? ? ? ? float g = Random.Range(0f, 1f);
? ? ? ? float b = Random.Range(0f, 1f);
? ? ? ? Color color = new Color(r, g, b);
? ? ? ? return color;
? ? }
}

给一个Cube销毁脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cubexiaohui : MonoBehaviour {

? ? // Use this for initialization
? ? void Start () {

? ? }

? ? // Update is called once per frame
? ? void Update () {
? ? ? ? if (this.transform.position.y < 0)//如果此物体的y坐标小于0,销毁此物体;
? ? ? ? {
? ? ? ? ? ? Destroy(this.gameObject);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? Destroy(this.gameObject,100);//或者100秒后销毁
? ? ? ? }
? ? }
}

球体预制体Sphere

给个反弹材质,挂载随机颜色脚本,挂载xiaohui脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class xiaohui : MonoBehaviour {
? ? AudioSource audio;//销毁的音效

? ? // Use this for initialization
? ? void Start () {
? ? ? ? audio = GetComponent<AudioSource>();//承接一下音效组件
? ? }
? ? void OnCollisionEnter(Collision coll)//被碰撞的形参
? ? {
? ? ? ? if (coll.gameObject.tag == "Player")//如果碰到标签为"Player"的物体,就销毁它
? ? ? ? {
? ? ? ? ? ? audio.Play();//播放音效
? ? ? ? ? ? Destroy(coll.gameObject);//销毁碰撞的物体
? ? ? ? }
? ? }
? ? // Update is called once per frame
? ? void Update () {
? ? ? ? Destroy(this.gameObject, 10);//此物体十秒后自己消失
? ? }
}

添加销毁音效,至此大功告成.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

相关文章

最新评论

?


http://www.vxiaotou.com