文章标题
来源:互联网 发布:马云 英文演讲 知乎 编辑:程序博客网 时间:2024/06/03 17:18
学习笔记一
通过制作一些简单小游戏来学习unity以及提高技能是一个不错方式,同时也可以写博客来提升一下写作技能,第一次写博客还有些不知所措。
首先说下这个小游戏的思路,通过射击方块使他在空中停留时间越长,分数越高。下面就是我要做的步骤了。(不得不说感觉这编辑器太好用了!)
第一步 我们先把场景搭好,同时准备好游戏中的物体:玩家,子弹,方块,墙。
这就是我们的场景了。
这就当作是我们的子弹了。
第二步 我们写脚本控制玩家移动,同时要控制玩家移动范围:只能左右移动,且不超出墙。
public class PlayerMove : MonoBehaviour { float h; public Transform left; public Transform right; void Start() { } // Update is called once per frame void Update() { h = Input.GetAxis("Horizontal"); transform.Translate(transform.right * h * 5 * Time.deltaTime); } private void OnCollisionEnter(Collision collision) { if (collision.collider.name == "Left") { transform.position = new Vector3(transform.position.x + 1f, transform.position.y, transform.position.z); } if (collision.collider.name == "Right") { transform.position = new Vector3(transform.position.x -1f, transform.position.y, transform.position.z); } }}
第三步 我们写子弹的发射脚本:向上发射,同时要控制摧毁子弹如果碰到物体后以及自然消失,并且如果子弹击中了方块,我们要统计击中的次数。
public class Fire : MonoBehaviour { public GameObject player; public GameObject shell; Vector3 shellPos; // Use this for initialization void Start() { } // Update is called once per frame void Update() { shellPos = new Vector3(player.transform.position.x,player.transform.position.y+0.2f,player.transform.position.z); if (Input.GetMouseButtonDown(0)) { Instantiate(shell, shellPos, player.transform.rotation); } } }
public class Shell : MonoBehaviour{ public Rigidbody shellRid; [HideInInspector] public static int count = 0; [Range(1, 10f)] public int speed = 5; // Use this for initialization void Start() { StartCoroutine(destroy()); } // Update is called once per frame void Update() { shellRid.velocity = transform.up * speed; } IEnumerator destroy() { yield return new WaitForSeconds(1.5f); Destroy(gameObject); } private void OnCollisionEnter(Collision collision) { if (collision.collider.name == "Cube") { Destroy(gameObject); count++; //Debug.Log(count); } Destroy(gameObject, 0.01f); }}
第四步 我们把方块的刚体加上,同时要使用重力,还要使z值相同,落下去后要复位。
public class EqualPosZ : MonoBehaviour { public Transform player; Vector3 pos; // Use this for initialization void Start() { pos = player.position; transform.position = new Vector3(transform.position.x, transform.position.y, pos.z); }}
public class Reset : MonoBehaviour { Vector3 oldPos; Quaternion rota; public GameObject cube; public Transform player; public Rigidbody rid; // Use this for initialization void Start() { oldPos = cube.transform.position; rota = cube.transform.rotation; } // Update is called once per frame void Update() { if (Vector3.Distance(gameObject.transform.position, player.position) > 20f) { cube.transform.position = oldPos; cube.transform.rotation = rota; rid.velocity = new Vector3(0, 0, 0); } }}
第五步 我们把ui显示脚本写上,显示击中次数,倒计时(倒计时到游戏结束),以及得分。
public class UI : MonoBehaviour { public Text countText; public Text scoreText; public Text timeText; [Range(60, 90)] public int totalTime = 60; int oldTime; // Use this for initialization void Start () { StartCoroutine(time()); oldTime = totalTime; } // Update is called once per frame void Update () { countText.text = "击中目标" + Shell.count.ToString() + "次"; scoreText.text = "现在得分为" + Shell.count * (oldTime - totalTime); timeText.text = totalTime.ToString(); } IEnumerator time() { while (totalTime > 0) { yield return new WaitForSeconds(1f); totalTime--; } }}
大概步骤就是如此。
其中也有不少问题还需要改进,比如UI显示的时候当最大化后,会有移位。
当然自己想加入你想要的功能都可以尝试自己往上面加。
新手勿喷!
阅读全文
1 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- LinuxPAServer19.0.tar.gz压缩包
- more effective C++条款八解读
- 欢迎使用CSDN-markdown编辑器
- django UnicodeDecodeError
- 改造自己的ubuntu主题为Mac OS
- 文章标题
- 使用Android Studio引入开源library出现Gradle sync failed: No service of type Factory<LoggingManagerInternal>
- MySQL5.7密码安全策略
- Android中ContentProvider的理解使用
- 在FPGA上实现H.264/AVC视频编码标准
- Android Studio 小技巧/快捷键 合集
- MongoDB 条件操作符说明
- springboot war包导出
- Comment.js模块暴露方法(exports、module.exports)的区别