Unity 滚球游戏

步骤一 创建滚球

  1. Hierarchy界面下创建一个球体(sphere),命名为Player;创建地面,命名为Plane。
  2. 给Player增加一个组件Rigidbody。
  3. Project界面下创建文件夹Scripts,在里面创建C#脚本PlayController。基本思想是按下方向键就施加向相应方向的力。由于是物理模型,可以用FixedUpdate()代替Update()保持不同帧率下的稳定性。代码如下:
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    public float speed;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        rb.AddForce(movement * speed);
    }
}
  1. 用VS编译,将此脚本拖到Player上。可以在Inspector界面下设置speed,即移动速度。进入game界面时按箭头移动球体。

步骤二 绑定相机

事实上,我们在Hierarchy界面下把相机拖到Player上就完成了绑定,通常这样是用作第一人称视角。这样的问题是当Player旋转的时候,相机也会跟着旋转,这样在进入game界面时,会有天旋地转的感觉。所以,尤其是第三人称视角,我们只需要相机和Player的相对位置保持一致。我们使用脚本CameraConller控制。代码如下:

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;
    }
}

这里解释一下transform。他是所有Object都有的组件,其中transform.position、transform.ratation、transform.scale分别存储了这个Object的位置、角度、大小。
将此脚本拖到MainCamera上,点击Inspector界面中Player右边的齿轮,选择Player。
这里采用LateUpdate(),这样他就时所有Update中最后更新的。

步骤三 建立墙壁

首先建立一个空物体,命名为Walls。再在Plane周围创建4面墙壁,隶属于Walls,分别命名为West Wall,East Wall,North Wall,South Wall。

步骤四 建立目标体

首先在脚本PlayerController中增加语句:

private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.CompareTag("Pick up"))
        {
            other.gameObject.SetActive(false);
        }
    }

这个函数的意思是如果Player如果碰到了某种触发器,就执行这个函数。而这个函数里面的意思是,如果这个other的触发标签(Tag)是"Pick Up",那么设置这个other物体不活跃,相当于该物体在Inspector界面下取消了第一个勾,从而设定为消失。
接下来就是设置Tag。首先我们在Project文件夹下创建Prefabs文件夹,建立Cube命名为Pick Up,可以涂为黄色。双击进入,从上向下找到Tag,选择Add Tag,点加号,并命名为Pick Up。回到主页面,建立空物体Pick Ups,在Pick Ups下使用若干Prefabs中的Pick Up,拷贝若干。结果如图:
在这里插入图片描述
最后进入Prefabs中的Pick Up中,在Box Collider中点击Is Trigger。创建Rigidbody,勾选Use Gravity和Is Kinematic。

步骤五 显示分数和结束游戏

进入PlayerController脚本,新建私有变量count用来记录分数。在start()函数中初始化为0。每吃到一个“Pick Up”,count+1。

...
private int count 
void Start()
{
    rb = GetComponent<Rigidbody>();
    count = 0;
}
...
 private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.CompareTag("Pick Up"))
    {
        other.gameObject.SetActive(false);
        count++;
    }
}

接下来的问题就是怎么显示这个分数了
在Hierarchy点create->UI->Text,重命名为Count Text。
在Inspector中Rect Transform菜单下点击大方块,按住Shift+Alt键选左上角。PosX改为10,PosY改为-10。
打开脚本PlayerController,增加代码。

...
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    public Text CountText;
    
    void Start()
    {
        ...
        SetCountText();
    }
    
    ...
    
    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.CompareTag("Pick Up"))
        {
            ...
            SetCountText();
        }
    }
    void SetCountText()
    {
        CountText.text = "Count: " + count.ToString();
    }
}

创建新的文本对象,重命名为Win Text,Paragraph中设为居中。PosY设为75。
增加代码如下:

public Text WinText;
void Start()
{
    ...
    WinText.text = "";
}
private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.CompareTag("Pick Up"))
    {
        ...
        if(count >= 12)
        {
            WinText.text = "You Win!";
        }
    }
}

点击Player,分别链接对应的文本。

步骤六 导出游戏

首先保存场景、保存项目。
File–>Build Setting
选择合适的平台,把场景文件拖到上方的框中(Scenes In Build)。点击Build,在项目中新建一个文件夹Build,选择此文件夹。点击Build and Run。

好了,大家愉快的玩游戏吧!

参考文献

https://www.w3cschool.cn/unity3d_jc/unity3d_jc-fntk2d6t.html
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/

代码

https://github.com/lyksunny/Unity/tree/master/RollBall

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值