Unity2D横版游戏地形生成

演示视频

横版地形生成

功能:
将地形素材赋值给脚本,脚本会生成对应的地形素材,可以控制生成素材的高度,生成的时间间隔,是否每次随机时间生成,会有一个范围值来进行控制:

在这里插入图片描述
Mountain 01_transf为山脉生成的起始位置,后续的生成变化都会以这个点位基准来进行变化
在这里插入图片描述

山脉脚本:

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

public class mountionMove : MonoBehaviour
{
    public float moveSpeed = 0;
    // Start is called before the first frame update
    void Start()
    {
        Destroy(this.gameObject, 10);
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(new Vector3(-1, 0, 0) * moveSpeed);
    }
}

生成器脚本:

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

public class Mountain01 : MonoBehaviour
{
    public GameObject mountain01;
    public Transform mountain01_transf;

    float time = 0;
    public float time1 = 0;

    public bool isRandom = false;

    int randomNum = 0;
    public int randomMin = 0;
    public int randomMax = 0;
    bool isRandomed = false;

    public bool isRandomHeight = false;
    public int reduceHeight = 0;
    public int addHeight = 0;

    bool isHeightRandom = false;
    int endHeight = 0;

    int A = 0;
    int B = 0;
    
    void Start()
    {
        A = reduceHeight;
        B = addHeight;
    }

    // Update is called once per frame
    void Update()
    {
        if(isRandom)//没有√就是不随机间隔
        {
            time = time + Time.deltaTime;
            if (time > time1)
            {
                Instantiate(mountain01, mountain01_transf.position+new Vector3(0,endHeight,0), Quaternion.identity);
                time = 0;
            }
        }else
        {
            if(isRandomed==false)
            {
                randomNum = Random.Range(randomMin, randomMax);
                isRandomed = true;
            }
            time = time + Time.deltaTime;
            if (time > randomNum)
            {
                isRandomed = false;
                Instantiate(mountain01, mountain01_transf.position+ new Vector3(0,endHeight , 0), Quaternion.identity);
                time = 0;
            }
        }
        
        if(isRandomHeight)//有√就是随机
        {
            if(isRandomed==false)
            {
                endHeight = Random.Range(reduceHeight, addHeight);
                isRandomed = true;
            }
            
        }else
        {

            endHeight = 0;
        }

        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值