unity伪贪吃蛇

模拟贪吃蛇游戏:

需要源代码,可自行查看资源下载。
共有一个地图,一个主体,一个模型,6个脚本
主体:globule
模型:PickUp
脚本:block、crete_model、globule_action、ground_tigger、timer、viewing_angle

一、地图

自行创建地面和墙体
本次游戏效果图如下:
在这里插入图片描述

二、模型

创造一个方形自定义材料添加刚体
在这里插入图片描述
创建c#脚本block作用于模型pickup

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

public class block : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        transform.Rotate(1, 1, 0);//用于模型旋转
    }
}

移下方建立模型命名为pickup并建立标签为PickUp在这里插入图片描述

三、脚本

建立c#脚本crete_model作用于Directional Light(平行光)
用于创建模型(由于Directional Light(平行光)处于中心)

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

public class crete_model : MonoBehaviour
{
    public GameObject obj;
    // Start is called before the first frame update
    void Start()
    {
        for(int i = 0;i<50;i++)
        { 	//随机在X,Z轴的100f内随机创建50个模型
            Instantiate(obj, new Vector3(Random.Range(-50f, 50f), 1 , Random.Range(-50f, 50f)), Quaternion.identity); 
           
        }
    }
    // Update is called once per frame
    void Update()
    {
    }
}

创建c#脚本作用于globule,用于小球移动

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


public class globule_action : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
    	//获取键盘输入,并按照指定位置移动5个单位
        if (Input.GetKey(KeyCode.D))
        {
            transform.position += new Vector3(Time.deltaTime * 5, 0, 0);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.position += new Vector3(-Time.deltaTime * 5, 0, 0);
        }
        if (Input.GetKey(KeyCode.W))
        {
            transform.position += new Vector3(0, 0, Time.deltaTime * 5);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.position += new Vector3(0, 0, -Time.deltaTime * 5);
        }
        if (Input.GetKey(KeyCode.Q))
        {
            transform.position += new Vector3(0, -Time.deltaTime * 5, 0);
        }
        if (Input.GetKey(KeyCode.E))
        {
            transform.position += new Vector3(0, Time.deltaTime * 5, 0);
        }
    }
   
}

创建c#脚本用于globule,用于判断其碰撞后的动作

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

public class ground_tigger : MonoBehaviour
{
	//总碰撞数
    int count=0;
    void Start()
    {
        
    }

   
    void Update()
    {
      


    }

    void OnCollisionEnter(Collision collision)
    {
        //碰撞PickUp使物体消失,并让总碰撞数加1
        if (collision.collider.tag == "PickUp")
        {
            Destroy(collision.collider.gameObject);
            count++;
            Debug.Log("得分:"+count);
        }

    }
}

创建c#脚本timer,对游戏计时。

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

public class timer : MonoBehaviour
{
    float alltime = 5;
    float atimer = 0;
    GUIStyle style = new GUIStyle();
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {	//当时间为负数时,游戏结束
        if (alltime != -1)
        {
            atimer += Time.deltaTime;
            if (atimer > 1)
            {
                Debug.Log("当前时间:" + alltime);
                alltime -= 1;
                atimer = 0;
            }
        }
        else
        {
            #if UNITY_EDITOR

            UnityEditor.EditorApplication.isPlaying = false;
            Debug.Log("游戏结束");
                #else
                Application.Quit();
                Debug.log("游戏退出");
                #endif
        }
        }
    }
}

创建c#脚本viewing_angle,作用于摄像头Main Camera,用于让其跟随小球移动。

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

public class viewing_angle : MonoBehaviour
{
    public Transform playerTransfrom;
    private Vector3 offset;
    void Start()
    {	//获取跟随物的位置
        offset = transform.position - playerTransfrom.position;
    }
    void Update()
    {	//摄像头相对于跟随物
        transform.position = playerTransfrom.position + offset;
    }

}

最终效果如图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值