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

原创粉丝点击