Project Training--Unity Multiplayer Game Development--Part Six

review

In the game Fantasy Jumping Game, we mainly explained the animation system and character control system of unity. The following mainly describes some other extensions of the game.

content

First, we're talking about platforms. In this miracle jump, we mainly use 5 different platforms and dangerous factors such as missiles and pendulums to enrich the fun and gameplay of the game. The following mainly explains these platforms and their functions. All platforms and shells move up over time, and will be destroyed when they reach a certain height.

//平台有多种,都需要执行这个移动代码
void Update()
    {
    
    
        MovePlatform();
    }

    //移动板子
    void MovePlatform()
    {
    
    
        transform.position += movement * Time.deltaTime;

        if (transform.position.y >= topLine.transform.position.y)//超过了TopLine的高度就摧毁、毕竟超出屏幕不影响游戏了、物体积攒起来会变卡所以摧毁
        {
    
    
            Destroy(gameObject);
        }

    }

In addition, there is a platform generator, which controls the generation of the platform and increases the risk factor of the platform

 public void SpwanPlatform()//计时方法、一定时间生成一个平台,没有到特定时间则仅仅积累时间不生成平台
    {
    
    
        countTime += Time.deltaTime;
        countTime2 += Time.deltaTime;
        spawnPosition = transform.position;//获取我自己的位置作为生成板子的位置、是这个脚本挂在了“startLine”物体身上,我/this=StartLine
        spawnPosition.x = Random.Range(-3.5f, 3.5f);//水平位置要随机一下
        if (countTime >= spwanTime)//到了时间就生成一个平台
        {
    
    
            CreatePlatform();
            countTime = 0;//置0
        }
        if (countTime2 >= spwanTime+1)
        {
    
    
            CreateMenacePlatform();
            countTime2 = 0;
        }
    }

 if(platNum == 0)//two platforms
        {
    
    
            int index2 = Random.Range(0, platforms.Count);
            if(System.Math.Abs(spawnPosition.x)>=1.5f)
                Instantiate(platforms[index2], new Vector3(-spawnPosition.x,spawnPosition.y), Quaternion.identity);
            else
                Instantiate(platforms[index2], new Vector3(spawnPosition.x-1.5f, spawnPosition.y-1), Quaternion.identity);
        }

common platform

First of all, what we want to talk about is the ordinary platform, which is also the simplest basic platform. This platform mainly provides players with the role of standing, without special effects.
insert image description here

temporary platform

The platform is mainly to provide a temporary stay. When you step on it, it will only exist for 3 seconds, and then it will be broken and no longer exist.
insert image description here

AudioSource breakAudioSource;
    public AudioClip breakClip;
    float countTime = 0, endTime = 1;
    bool start = false;//控制是否需要计时
    void Update()
    {
    
    
        if(start)
        {
    
    
            countTime += Time.deltaTime;
        }
        if(countTime>=endTime+1)
        {
    
    
            Destroy(gameObject);
        }
        if (countTime >= endTime)
        {
    
    
            transform.position = new Vector3(0, -20);
        }
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {
    
    
    //一旦进行了玩家触碰,就需要倒计时进行销毁。并且进行音效触发。
        if (collision.collider.gameObject.tag.Equals("Player"))
        {
    
    
            breakAudioSource = GetComponent<AudioSource>();
            breakAudioSource.clip = breakClip;
            breakAudioSource.PlayDelayed(1);
            start = true;
        }
    }

fan platform

The platform has two effects. The first one is that when the player touches the platform, there will be a bouncing effect. It is necessary to carefully consider whether to step on it, because if the player reaches the highest height, he will touch the top spike and cause death . The other is that when the player touches it, the fan will automatically rotate and perform a rotation animation.

 private void OnCollisionEnter2D(Collision2D other)
    {
    
    
//执行旋转动画
        if (other.collider.gameObject.tag.Equals("Player"))
        {
    
    
            animator.Play("Fan_run");
        }
    }

The player leaps, touching the fan platform, causing the player to jump.

 private void OnCollisionEnter2D(Collision2D other)//碰撞检测
    {
    
    
        if (other.gameObject.CompareTag("Fan"))//如果和一个标签为“Fan”的平台碰撞了,那玩家保持原有水平方向的速度的同时获得一个向上的10float的速度。
        {
    
    
            rb.velocity = new Vector2(rb.velocity.x, 10f);
        }
	}

Summarize

This time it mainly talks about the platform generator and three simple platform effects, the impact caused by the player's touch, and the remaining platforms will be described next time.

Guess you like

Origin blog.csdn.net/qq_53259920/article/details/125135995