Unity2D横版游戏地形生成

Posted Z_hongli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
        

        
    


以上是关于Unity2D横版游戏地形生成的主要内容,如果未能解决你的问题,请参考以下文章

unity2D横版游戏教程5-UI

unity2D横版游戏教程-1 让人物动起来

1.Unity2D 横版 移动+跳跃+跳跃优化+长按高跳

2.Unity2D 横版 帧动画sprite animation+动画状态机animator+丝滑连击动作

Unity 3D 地形系统概述|| Unity 3D 创建地形

unity实战:教你做黄豆君