Unity环境下制作小球移动吞噬方块的小游戏

  • 制作内容

小球移动吞噬转动的方块

  • 制作工具

Unity、Vscode

  • 制作步骤

1、新建项目,下载材质素材,创建文件夹(scenes场景、脚本scripts、prefabs预制件),基本设置调整(摄像机调高,45°,取消允许HDR渲染)

2、用Plane建场地,命名为Ground,导入材质文件。

3、做墙体:

建立空游戏对象Gameproject并命名wall——添加4个Cube调整大小位置作为东南西四边的墙eastwall、westwall、southwall、northwall,可把wall这个墙体作为预制件放入prefabs

4、添加一个cube对象命名pickup——添加材质,添加Tag,定义一个Tag-pickup(编程时要引用),添加rigidbody(打勾Is  Trigger检测碰撞、Is Kinematic使它不受重力影响),写旋转脚本rotatepickup,把设置好的pickup作为预制件

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

public class RotatePickUp : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        this.transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);//Y轴
    }
}

5、在场景中加入12个pickup预制件(设置空游戏对象pickups,放进去一个pickup预制件,复制11个)把12个pickup排成一圈

6、创建一个Sphere对象命名为player——添加rigidbody,添加运动脚本playercontroller)并添加两个text<显示碰撞个数(ui-text)命名counttext,wintext,设置锚点,位置,在playercontroller脚本里添加相关代码

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

public class PlayerController : MonoBehaviour {
    public float speed;
    private Rigidbody rb;
    public Text countText;
    public Text winText;
    private int count;
    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody>();
        count = 0;
        winText.text = " ";
        SetCountText();
    }
    void FixedUpdate()//控制横向纵向
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
        rb.AddForce(movement * speed);
    }

    void OnTriggerEnter(Collider other)//吃掉
    {
        if(other.gameObject .CompareTag ("PickUp"))
        {
            other.gameObject.SetActive(false);
            count += 1;
            SetCountText();
        }
    }

    void SetCountText()
    {
        countText.text = "Count:" + count.ToString();
        if(count>=12)
        {
            winText.text = "You Win";
        }
    }
    // Update is called once per frame
    
    void Update () {
        
    }
}

7、写摄像机脚本cameracontroller

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

public class CameraController : MonoBehaviour {
    public GameObject player;
    private Vector3 offset;
    public GameObject pickupPfb;
    //private GameObject obj1;
    private GameObject[] obj1;//自动添加12个放在一个数组里
    private int objCount = 0;
    // Use this for initialization
    void Start() {
        offset = this.transform.position - player.transform.position;
        //obj1=GameObject.Instantiate(pickupPfb);
        //obj1.transform.position=new Vector3(5,2,3);//自动添加一个
        //自动添加12个
        obj1 = new GameObject[12];
        for(objCount =0;objCount<12;objCount++)
        {
            obj1[objCount] = GameObject.Instantiate(pickupPfb);
            obj1[objCount].name = "pickup" + objCount.ToString();
            obj1[objCount].transform.position = new Vector3(4 * Mathf.Sin(Mathf.PI / 6 * objCount), 1, 4 * Mathf.Cos(Mathf.PI / 6 * objCount));
        }
    }
    void LateUpdate()
    {
        this.transform.position = player.transform.position + offset;
    }

    // Update is called once per frame
    void Update () {
        
    }
}

8、也可设计自动生成12个转动的方块

评论

暂无评论