Unity官方实例教程 Roll-a-Ball
2017-05-17 14:53
417 查看
与unity的transform组件相处的挺久了,最近项目不太忙,决定好好打下unity的基础。那么从Roll-a-Ball这个简单游戏开始吧!1.先创建一个球体游戏对象,改名为Player,transform值y=0.5,加上刚体组件,具体如下图:
其中脚本PlayerController.cs代码如下:
3.摄像机,让相机简单跟随游戏对象,为Main Camera挂一个脚本CameraController,代码如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/14/a8b30aafeb2ba3a4be16f435312f112d.png)
using UnityEngine; using UnityEngine.UI; public class PlayerController : MonoBehaviour { public float speed; public Text countText; private float count; // Use this for initialization void Start () { count = 0; setCountText(); } // Update is called once per frame void FixedUpdate () { float moveH = Input.GetAxis("Horizontal"); float moveV = Input.GetAxis("Vertical"); Vector3 move = new Vector3(moveH,0.0f,moveV); GetComponent<Rigidbody>().AddForce(move * speed * Time.deltaTime); } void OnTriggerEnter(Collider other) { Debug.Log("碰撞"); if (other.gameObject.tag == "PickUp") { other.gameObject.SetActive(false); count++; setCountText(); } } private void setCountText() { countText.text = "Count:" + count; } }2.创建一个平面,改名为Ground,具体如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/14/6d9806fefaf7954721639177b41c28dd.png)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { public GameObject player; private Vector3 offset; // Use this for initialization void Start () { offset = transform.position; } // Update is called once per frame void LateUpdate () { transform.position = offset + player.transform.position; } }4.创建被抓取的立方体,通过预制体来做,先创建一个立方体,transform中rotation三个值都设为45,挂上脚本Rotator,代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rotator : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { transform.Rotate(new Vector3(15,30,45) * Time.deltaTime); } }然后将这个cube拖到Assets下面预制体文件夹中,再通过这个预制体来创建多个。6.再简单的添加一个计分板,create-UI-Text,并拖拽到Player的countText参数下,调整合适位置
Ground加墙什么的可以自由发挥,都比较简单。。。 比较重要的是理解Rigidbody,Collider,Is Trigger这三个东西,具体看这个总结:http://blog.csdn.net/monzart7an/article/details/22739421
相关文章推荐
- 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官方内置网络unet的实例教程(一)
- [置顶] unity官方内置网络unet的实例教程(六)
- Unity官方实例教程 Space Shooter
- 【Unity3D 案例Demo】官方视频教程Roll A Ball总结
- Unity官方游戏实例视频中文教程之太空射击学习笔记
- Unity官方实例教程 Space Shooter
- unity官方内置网络unet的实例教程(八)
- [置顶] unity官方内置网络unet的实例教程(三)
- unity官方demo学习:Roll-a-Ball
- 对《[Unity官方实例教程 秘密行动] Unity官方教程《秘密行动》(十二) 角色移动》的一些笔记和个人补充,解决角色在地形上移动时穿透问题。
- [置顶] unity官方内置网络unet的实例教程(七)
- Unity 3D 官方教程—— Roll A Ball 学习记录
- unity官网教程——Roll-a-ball tutorial——1移动的小球
- [置顶] unity官方内置网络unet的实例教程(四)