Unity官方练习——Roll-a-ball tutorial

Introdution

https://unity3d.com/cn/learn/tutorials/projects/roll-ball-tutorial

![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/bb8abb0d5146d495c69130641b28c0af.png)

游戏场景(Scene)

基本环境——四个墙面围起来的平面

![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/fb34eb04116ccb1751f5adab24a7617c.png)
  • 基本模型建立(平面Plane和立方体Cube)。建立完一个对象规范命名,对transform组件reset。
  • 位置的设定和调整(利用Q W E R F快捷键)

游戏主体——小球和其他立方体

![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/021e9faeeda7ae970f335cb357d22cd0.png)

文件管理规范——在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

![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/6058360996629b37c7da70829efc514b.png)

游戏运行时UI——Count:x

![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/ede804f3ce63639ce9c6e1062fda423b.png)

实现方法

  • 用一个变量保存玩家分数:在球体脚本中添加一个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属性
![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/9bf7f6e48db9ce1cbe0b9dab79f3bde2.png)

===

学习参考:
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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值