1. 新建一个项目
2. 新建一个平面对象
3. 新建一个材质球
为材质球设置颜色
5. 将材质球拖拽赋值给平面
6. 创建立方体对象,拉伸至平面宽度
7. 再复制三个立方体、调整位置、添加材质,用四个调整过的立方体将平面围起来
8. 创建一个球体(游戏的主角)
9. 创建一个圆柱体并调整为金币形状、竖着摆放在桌面上
10. 创建一个材质球,取金色,并赋值给金币
Ctrl D复制多个金币(共10个),调整位置,围绕小球布局。
11. 创建一个空对象,将所有金币放到空对象中,以便统一管理(立方体也是)
12. 为金币添加脚本,实现自动旋转
金币的自转脚本 rorate.cs(将脚本手动拖拽挂载到每一个金币上)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class rorate : MonoBehaviour
{void Start(){}void Update(){this.transform.Rotate(Vector3.right, Space.Self);//自转}
}
13. 为小球添加刚体组件(检查器:添加组件→Physics→Rigidbody)
14. 为小球添加脚本,实现通过键盘WSAD、↑↓←→控制小球移动
此时小球碰到金币会弹开
小球的移动脚本 move.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class move : MonoBehaviour
{Vector3 mPrevPos = Vector3.zero;Vector3 mPosDelta = Vector3.zero;// Start is called before the first frame updatevoid Start(){}void Update(){float ws = Input.GetAxis("Vertical"); //按‘W’'S'键返回一个[-1,1]的值float ad = Input.GetAxis("Horizontal"); //按‘A’'D'键返回一个[-1,1]的值this.transform.Translate(Vector3.forward * ws * Time.deltaTime * 7, Space.World); //前后移动transform.Translate(Vector3.right * ad * Time.deltaTime * 7, Space.World); //左右移动}
}
15. 为每一个金币设置自定义的标签(coin),作为碰撞检测的判断条件
新加自定义标签:
16. 碰撞检测。为小球添加脚本实现“小球碰到金币 -> 金币消失”(即小球吃金币)功能
小球吃金币(小球碰到金币后金币消失,且层级中的金币对象也消失):
小球的碰撞检测脚本 collision.cs(拖拽挂载给小球)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;public class collision : MonoBehaviour
{private Rigidbody rd;public int force=5;void Start () {rd = GetComponent<Rigidbody> ();}void Update () {float h = Input.GetAxis ("Horizontal");float v = Input.GetAxis ("Vertical");rd.AddForce (new Vector3 (h, 0, v) * force);}//碰撞检测void OnCollisionEnter(Collision collision){if (collision.collider.tag == "coin") {Destroy (collision.collider.gameObject);}}
}
17. 触发检测(与16 碰撞监测二选一)
全选金币,勾选“是触发器”,即触发机制的开关
添加脚本,实现小球触到金币后,金币消失,且层级中的金币也对应消失
小球的触发检测脚本 trigger.cs(拖拽挂载给小球)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class trigger : MonoBehaviour
{private Rigidbody rd;public int force=5;void Start () {rd = GetComponent<Rigidbody> ();}void Update () {float h = Input.GetAxis ("Horizontal");float v = Input.GetAxis ("Vertical");rd.AddForce (new Vector3 (h, 0, v) * force);}//触发检测void OnTriggerEnter(Collider collider){if (collider.tag == "coin") {Destroy (collider.gameObject);}}
}
18. 为相机添加脚本,实现相机跟随小球
脚本参数 target赋值为小球,即跟随对象为小球
相机跟随小球:
相机脚本 follow.cs(脚本拖拽挂载给相机,脚本中的变量“target”赋值为小球)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class follow : MonoBehaviour
{public Transform target;private Vector3 offset;void Start () {offset = transform.position - target.position;}void Update () {transform.position = target.position + offset;}
}
19. 添加实时分数文本(TextMeshPro)
修改颜色、大小、初始内容。
20. 完善小球collision.cs脚本实现实时修改得分
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;public class collision : MonoBehaviour
{private Rigidbody rd;public int force=5;private int score_num = 0;public GameObject score;void Start () {rd = GetComponent<Rigidbody> ();}void Update () {float h = Input.GetAxis ("Horizontal");float v = Input.GetAxis ("Vertical");rd.AddForce (new Vector3 (h, 0, v) * force);}//碰撞检测void OnCollisionEnter(Collision collision){if (collision.collider.tag == "coin") {score_num++;score.GetComponent<TMP_Text>().text = "score: " + score_num.ToString ();Destroy (collision.collider.gameObject);}}
}
21. 添加游戏胜利文本并修改颜色、内容
22. 完善小球collision.cs脚本,实现吃到10个金币时文本出现(未激活 -> 激活)
修补collision.cs 代码(最终版):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;public class collision : MonoBehaviour
{private Rigidbody rd;public int force=5;private int score_num = 0;public GameObject score;public GameObject win;void Start () {rd = GetComponent<Rigidbody> ();win.SetActive (false);}void Update () {float h = Input.GetAxis ("Horizontal");float v = Input.GetAxis ("Vertical");rd.AddForce (new Vector3 (h, 0, v) * force);}//碰撞检测void OnCollisionEnter(Collision collision){if (collision.collider.tag == "coin") {score_num++;score.GetComponent<TMP_Text>().text = "score: " + score_num.ToString ();if (score_num == 10) {win.SetActive (true);}Destroy (collision.collider.gameObject);}}
}
23. 发布游戏
文件菜单栏 -> 构建和运行
选择游戏生成位置文件夹
选择文件夹,片刻后发布成功
24. 运行游戏
进入游戏生成的文件夹,找到对应的exe文件,打开即可全屏进行游戏
启动页
游戏运行:
吃掉全部金币胜利: