Unity Development Diary [Day 9] - Character Death, Scene Switching and Lighting Effects

Table of contents

1. Character death

2. Scene switching 

 3. 2D light effect

 4. Optimization


1. Character death

Realizing the player's death is a very important thing for any game. Next, we will try this effect. First of all, there may be many conditions for a character to die, such as being attacked by a monster, a trap, or falling from a high place. We First, let's implement the mechanism of falling from a height.

There are many ways to implement it. Here I want to use a trigger as the dead line. When the player touches the dead line, it will die immediately. So first create an empty project, then add a collision body, and change it to a trigger. and adjust its size 

Then we go to the Player's script to write the code [Note: We can use the collision function of the previous collection here, which can be reused better], after the player dies, we need to restart the game scene, which requires a new function , so we need the new header file

using UnityEngine.SceneManagement;

Then we use the scene restart function to realize the resurrection of the player, the code is as follows:

/* 角色下落死亡 */
if(collision.tag == "deadline")
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().name); //重启当前场景
}

 We use the GetActiveScene function to get the name of the current scene, which avoids the problem that we need to change the code in different game scenes because we directly use the name of the scene. But after testing, we found that this reset speed is very fast, and we want to have a delay effect.

The implementation method is very simple. After confirming that the death condition is met, it will no longer die immediately, but execute the Invoke function first and delay for a period of time, and then execute the ReStart function.

Invoke("ReStart", 0.5f); //延迟后执行死亡函数

2. Scene switching 

Next, we want to switch from one game scene to another. My idea is to add a piece of code to load the next scene where we triggered the next level dialog box before. This code will be executed when E is pressed. Load the next scene (SceneManager.GetActiveScene().buildIndex can get the number of the current scene)

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

public class EnterTwo : MonoBehaviour
{
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.E)) //当按下E的时候
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1); //加载下一个场景
        }
    }
}

Where do we set or sort this scene number?

We open the file > build settings and we can see it, and we can also import our other game scenes here. (You can directly drag in the established scene)

I decided to load the code into this UI that is closed by default, so as to avoid the bug that the player accidentally touches the E key during the game and directly loads the next level

 3. 2D light effect

In the second game scene, I made a special cave, so we need to add some light effects to it to make it look better. First, change the material of the map and background to Default - Diffuse so that our scene has darkened

At this time, we found that the character is still bright because no material has been added. We need to change the material, and then after everything in the scene is darkened, we start to try to add light sources (the game is a 2D game but the light source is 3D, so there may be added The point light source has no light, and the Z axis needs to be adjusted. Similarly, after adding materials, we may not see the original objects in the scene, and the Z axis needs to be adjusted)

In this way, we have realized the light source, and we can add a light source to the Player by the same principle

 

That's it. 

 Note: Directional lights can be used here to illuminate the entire scene.

 4. Optimization

 I added a collection animation to all the items. The production of the animation is very simple. The added logic is the same as the logic of the monster’s death, so we deleted the code in the Player and moved it to each collection item. (Because there are only two collections, I implemented them separately, and did not implement them together with classes)

Collection script: The code here means to play the collection animation when the player triggers it. After the animation is played, the event will be called to execute the Destory function

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

public class Cherry : MonoBehaviour
{
    public Animator Anim;
    public Collider2D Coll;

    private void OnTriggerEnter2D(Collider2D collision) //碰撞器触发函数
    {
        if(collision.tag == "Player")
        {
            Anim.SetTrigger("getting");
        }
    }

    public void Destory()
    {
        Destroy(gameObject);
    }
}

 Character controlled script:

    private void OnTriggerEnter2D(Collider2D collision) //碰撞器触发函数
    {
        /* 物品收集 */
        if(BodyColl.IsTouching(collision) && collision.tag == "cherry") //樱桃收集
        {
            collision.enabled = false;
            CherryAudio.Play();
            CherryCount++; //收集品数量加一
            CherryNumber.text = CherryCount.ToString();
        }
        if (BodyColl.IsTouching(collision) && collision.tag == "gem") //宝石收集
        {
            collision.enabled = false;
            GemAudio.Play();
            GemCount++; //收集品数量加一
            GemNumber.text = GemCount.ToString();
        }
    }

Here, after confirming the collision, first close the collider to prevent repeated triggering during animation playback.

Guess you like

Origin blog.csdn.net/qq_50688324/article/details/127071242