游戏制作之入门游戏

用Unity制作一款接苹果的小游戏十分的简单

这个是游戏的展示视频链接:

https://www.bilibili.com/video/BV1DYw6e8EVs/?spm_id_from=333.1387.upload.video_card.click&vd_source=2031500f1882f9295697f4d29fcdccf6

以下是具体的步骤

  1. 创建一个名为“A1”的新3D项目。Unity项目的默认位置在计算机的“我的文档”中,您可以将其更改为其他位置。

  2. Unity为每个新项目打开一个默认的空场景,尽快保存并重命名这个场景。

  3. 在层级窗口(Hierarchy window)中选择“Main Camera”,配置背景颜色和投影方式如下:

4.选择菜单项“GameObject > Create Empty”来创建一个名为“Tree”的空对象,并设置其变换如下:

这里的“Tree”对象将被用作一个容器,目前它仅包含“Transform”组件。稍后您会为它添加一个脚本组件。
5. 选择菜单项“GameObject > 3D Object > Sphere”来创建一个名为“Foliage”的球体对象。在层级窗口(Hierarchy window)中,将“Foliage”拖拽到“Tree”上作为其子对象,并设置其变换如下:

6.在项目窗口(Project window)中点击“Create”,选择“Materials”子文件夹,创建一个绿色的材质并将其应用到“Foliage”上。之后创建的所有其他新材质也应放在这个子文件夹中。

7.选择“GameObject > 3D Object > Cylinder”来创建一个名为“Trunk”的圆柱体对象,并将其作为“Tree”的子对象。创建一个棕色的材质并应用到“Trunk”上。保持变换组件的默认设置:

8.选择菜单项“GameObject > 3D Object > Cube”来创建一个名为“Basket”的立方体对象,并应用一个黄色的材质,设置其变换如下(左侧)。右侧的图像显示了到目前为止的场景层级结构。

9.创建另一个名为“Apple”的球体对象。创建一个红色的材质并应用到它上面。为“Apple”添加一个RigidBody组件。将“Apple”对象拖拽到项目窗口中,使其成为一个预制件(Prefab),并将其移动到“Prefabs”子文件夹中。删除场景窗口中的“Apple”对象。

10.保存场景。

11.通过选择“GameObject > UI > Legacy > Text”创建UI文本,调整字体大小、颜色和位置,如下面的图中圈出的部分所示。现在你会在层级窗口(Hierarchy window)中看到一个新项目,位于“Canvas”节点下。所有UI对象都作为Canvas的子对象保留(屏幕空间对象)。

12.在项目窗口的“Scripts”文件夹中,创建一个名为“AppleTree”的C#脚本。注意脚本在创建时必须正确命名,因为脚本文件名必须与类名完全一致。将“AppleTree”脚本拖拽到层级窗口中的“Tree”对象上(而不是它的子对象)。

“AppleTree”的C#脚本内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AppleTree : MonoBehaviour
{
    public GameObject applePrefab;
    public float speed =0.1f;
    public float edge=16.0f;
    public float chanceToChange=0.01f;
    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("DropApple",1f,1.0f);
    }
    void DropApple()
    {
        Vector3 pos=transform.position;
        pos.z+=2.0f;
        pos.y -= 2.0f;
        GameObject apple=Instantiate(applePrefab,pos,Quaternion.identity);
        Destroy(apple,2.0f);
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.right*speed*Time.deltaTime);
        if(transform.position.x<-edge||transform.position.x>edge)
        {
            speed *= -1;
        }
    }
    void FixedUpdate()
    {
        if (chanceToChange > Random.value)
            speed *= -1;
    }
}

13.选择“Tree”对象,将上面创建的“Apple”预制件拖拽到“Apple Prefab”插槽中。

14.创建一个名为“Basket”的C#脚本,并将其应用到“Basket”对象上。

“Basket”的C#脚本的内容如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Basket : MonoBehaviour
{
    // Start is called before the first frame update
    public float edge = 16;
    private int numApple;
    public Text strApple;
    void Start()
    {
        numApple = 0;
        strApple.text = "Apple Picked:" + numApple.ToString();
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 posWorld;
        posWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector3(Mathf.Clamp(posWorld.x, -edge, edge), transform.position.y, transform.position.z);
    }
    private void OnCollisionEnter(Collision other)
    {
        Destroy(other.gameObject,0.5f);
        numApple++;
        strApple.text = "Apple Picked:" + numApple.ToString();
    }
}
    • 点击“Basket”对象,将“Canvas > Text”对象拖拽到“Str Apple”插槽中(见下图)。

    • 点击“Play”按钮运行游戏,确保所有错误都已修复。

    • 通过“File > Build Settings”构建一个可执行文件。

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值