Unity官方练习——Roll-a-ball tutorial
来源:互联网 发布:知学学院宋剑勇 编辑:程序博客网 时间:2024/06/10 10:35
Introdution
https://unity3d.com/cn/learn/tutorials/projects/roll-ball-tutorial
游戏场景(Scene)
基本环境——四个墙面围起来的平面
- 基本模型建立(平面Plane和立方体Cube)。建立完一个对象规范命名,对transform组件reset。
- 位置的设定和调整(利用Q W E R F快捷键)
游戏主体——小球和其他立方体
文件管理规范——在Project视图中建立文件夹管理;在Hierarchy视图中建立Empty来管理。
- 创建小球模型(位置大小、光照、重力组件、脚本组件)
- 利用预制(Prefab)创建多个立方体
//控制相机随球移动using UnityEngine;using System.Collections;public class CameraController : MonoBehaviour { public GameObject player; private Vector3 offset; void Start () { offset = transform.position - player.transform.position; } void LateUpdate () { transform.position = player.transform.position + offset; }}
游戏任务——移动球体拾取方块
- 移动球体
- 刚体组件:刚体可以给球体添加物理效果,让他可以检测物理碰撞,便于实现拾取物体和碰撞墙面
using UnityEngine;using System.Collections;public class PlayerController : MonoBehaviour{ //球体移动公有属性speed public float speed; //球体添加刚体组件,实例化一个对象用于控制移动和碰撞 private Rigidbody rb; //Start()方法只被调用一次(粗略理解) void Start() { rb = GetComponent<Rigidbody>(); // 一般初始化私有属性,公有属性可以直接在Unity编辑器中修改 } //Update()方法每帧被调用 //void Update() //{ // ... //} //处理Rigidbody时,需要用FixedUpdate代替Update。例如:给刚体加一个作用力时,你必须应用作用力在FixedUpdate里的固定帧,而不是Update中的帧。(两者帧长不同) void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); //Vector3 是一个类,表示3D的向量和点 Vector3 movement = new Vector3(moveHorizontal, 0,0f, moveVertical); //刚体类Rigidbody的方法, 标量和向量相乘 rb.AddForce(movemoment * speed); }
- 拾取方块机制:
function OnTriggerEnter(other: Collider): void
:当Collider(碰撞体)进入Trigger(触发器)时调用OnTriggerEnter - 球体为碰撞体,碰到的物体为触发器
- 设定方块为触发器,而不是碰到所有的物体都触发
- Tag标记的使用,给方块设定Tag为“PickUp”
void OnTriggerEnter(Collider other){ // 这里other变量代表球体碰到的对象,在这个方法里面,我先用if语句判断碰到的对象的Tag是否等于“PickUp”,如果等于,我就将碰到的对象设置为不激活(不显示) if (other.gameObject.tag == "PickUp") { other.gameObject.SetActive(false); }}
游戏UI界面——简单计分板
初始界面UI——Scores
游戏运行时UI——Count:x
实现方法
- 用一个变量保存玩家分数:在球体脚本中添加一个count计数变量
- 将分数显示在UI上面:创建文本UI,加入到脚本中新建立的countText属性中
using UnityEngine;using System.Collections;//UI 控件命名空间using UnityEngine.UI;public class PlayerController : MonoBehaviour{ public float speed; private Rigidbody rb; private float count; public Text countText; void Start() { rb = GetComponent<Rigidbody>(); count = 0.0f; //setCountText()方法用来更新countText的内容 setCountText(); } void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizonal, 0.0f, moveVertical); rb.AddForce(movement * speed); } void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "PickUp") { other.gameObject.SetActive("false"); count++; setCountText(); } } void setCountText() { countText.text = "Count:" + count; }}
- 引入命名空间UnityEngine.UI,以便使用Text类型的变量,
- 定义countText变量来保存UI Text
- 在Start方法中调用setCountText方法,来更新countText的内容
- 在每次球体碰到方块的时候,也需要更新countText的内容
- 定义setCountText方法
- 保存代码,回到Unity编辑器里面,将Text拖入到Player里面的Count Text属性
———-
学习参考:
0、https://unity3d.com/cn/learn/tutorials/projects/roll-ball-tutorial
1、http://www.jianshu.com/p/97b630a23234#
2、https://docs.unity3d.com/ScriptReference/Vector3.html
3、http://docs.manew.com/Script/index.htm
4、http://blog.csdn.net/u014230923/article/details/51320814
阅读全文
0 0
- Unity官方练习——Roll-a-ball tutorial
- Unity官方教程Roll-a-ball (一)
- Unity官方教程Roll-a-ball (二)
- unity官方demo学习:Roll-a-Ball
- Unity 官方案例 Roll a ball
- Unity 3D 官方教程—— Roll A Ball 学习记录
- 学习U3d官方项目——Roll-A-Ball
- Unity官方实例教程 Roll-a-Ball(二)
- Unity官方实例教程 Roll-a-Ball(一)
- Unity官方实例教程 Roll-a-Ball(二)
- Unity官方实例教程 Roll-a-Ball 学习笔记
- Unity制作Roll-a-Ball游戏
- Unity官方教程学习笔记之Roll A Ball篇---(二)创建地面
- Unity官方实例教程 Roll-a-Ball 学习笔记(一)+(二)
- Unity官方教程学习笔记之Roll A Ball篇---(一)创建工程和保存场景
- Unity3d 5.4-官方教程1之Roll-a-ball
- ROLL A BALL 小游戏
- Roll a Ball
- centOS6.8安装vsftp出现ftp:connect:Connection refused原因
- Struts2框架搭建及常见配置问题
- 业界应用,技术分享 record_1
- 关于requests项目
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
- Unity官方练习——Roll-a-ball tutorial
- 114. Flatten Binary Tree to Linked List
- HTML4结构
- POJ 1569 Myacm Triangles 笔记
- linux下vsftp安装完无法显示文件原因
- 淘淘商城系列——解决KindEditor上传图片浏览器兼容性问题
- POJ 1579 Function Run Fun 笔记
- HTML4 排版标记
- HTML4文本标记