Unity3D RPG implementation 1

 

 

content

Environment construction

Install URP

add skybox 

Set shadow rendering

Modify lighting rendering

Environment construction

Expand the map

implement navigation

Mouse control character movement 

mouse pointer toggle

camera follow

post-processing

animation controller

occlusion culling


Environment construction

Install URP

Install URP first

↑Install the unity package of URP

Create a new rendering pipeline:

Set the rendering pipeline in edit:

By upgrading the URP, you can make up for the lost material of the character.

 

add skybox 

Then in rendering-lighting-environment you can replace the skybox

The skybox can then be set here:

Set shadow rendering

This setting can set the distance of the maximum rendered shadow, beyond this distance will not be rendered, which can save resources

Here you can adjust various parameters of the shadow:

This place can modify the virtual and real shadows into several levels. For example, here, the shadows rendered by more than 1 level 12.5m will be more virtual. 

Note that HDR should be turned on to facilitate post-processing

Antialiasing can also be added here

Modify lighting rendering

Add lighting files in window-lighting:

The modification mode is baking, and the graphics is GPU 

Then click Generate Lights in the lower right corner:

Then click Generate Lighting:

Generally do not check the automatic generation

 

Notice that the light turns blue-purple at this time, because the light defaults to the color of the skybox, which can be modified here in the environment:

Environment construction

Press and hold v to better set the scene: ctrl+shift can also be adsorbed

Adjusting the camera angle can be done by first finding the angle in the scene, with the camera selected, and then pressing ctrl+shift+f to align the camera:

 An empty object can be created as a dividing line, the following objects are all characters:

 Install polybrush and import the shader with urp in the sample

 

Open in tools: 

When adjusting the height here, you can pass around the xyz axis or the surface of the object

This can achieve multiple symmetrical brushes

Soften makes the undulations smoother

The third brush color, the direct brush color cannot be colored because the ground is not the material of the polybrush

If you want to color, you can right-click on the shader in the material that comes with polybrush and add a material:

Then support the material to the ground, and you can color it at this time

This can be used to initially plan what the different places will be used for 

If you want the whole scene to become a certain color, select flood

the fourth

After putting the prefab on it, you can click and pop out the prefabs:

  Hold ctrl and click to delete

Expand the map

Next, we expand the map, but simply expanding the size will not increase the number of vertices of the object. Here you can use the plugin: probuilder:

 Open in tools,

You can view its details while holding shift

Hold alt to select the one above 

Its shape can be modified here

 

add plane to this

Note, however, that the center of this object is in the corner:

Click this to bring it back to the center

The use of progrid:

enable first

A grid showing the x or y or z plane can also be implemented.

The 2 here represents a grid of 0.2, and the moving object is also implemented according to a movement of 0.2.

Can be turned off in tools

 

Clicking this button changes the grid plane from four to three, which can be colored by creating a material:

implement navigation

Baking Ground:

Select navigation static for ground 

Also select the area of ​​the tree, but select not walkable when baking:

After the baking is successful, the non-walkable area is not fine enough

to change this

Add an agent to the player

 In order to make the non-walkable area finer, it is necessary to adjust the fat, thin, and height of the characters.

And also modify the navigation part:

The above is a static obstacle, which cannot be moved. If you want to achieve a dynamic obstacle, you can add obstacle components to the object, and check the carve, you can realize that this area will be cut when baking.

Mouse control character movement 

Create a mouse click event, drag the character in, and select the destination

Then use ScrennPointToRay to get the position of the mouse click

Then set the tag to the ground and add this code to the MouseManager:

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

[System.Serializable]
public class EventVector3 : UnityEvent<Vector3> { }//声明一个事件
public class MouseManager : MonoBehaviour
{

    public EventVector3 OnMouseClicked;
    RaycastHit hitInfo;
    private void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//获取射线
        Physics.Raycast(ray, out hitInfo);//将射线信息输出给hitInfo
        MouseControl();
    }
    void SetCursorTexture()
    {

    }
    void MouseControl()
    {
        if (Input.GetMouseButtonDown(0) && hitInfo.collider != null)
        {
            if (hitInfo.collider.CompareTag("Ground"))
            {
                OnMouseClicked?.Invoke(hitInfo.point);//点击之后将该位置传给OnMouseClicked设置的位置
            }
        }
    }
}

It can be realized that the character goes to the place where the mouse is clicked.

Speed ​​and direction can be adjusted here:

Auto braking achieves braking effect.

The above can realize the way that the mouse clicks the character to move, but you need to drag the character in

If you switch scenes or create a new game or multiple scenes in the future, it is inconvenient to drag and save. Next, we will introduce another way to register the function of character movement in the event. Whether switching scenes or creating new characters, the characters can be Add to this event of OnMouseClicked.

In order to change the above, MouseManager will be set to singleton mode here

Use the event method to replace the method in the previous comment,

 The way the event event is used is that someone else needs to register it.

When this method is called, all functions registered for this event will be called!

Next, write a MoveToTarget function in PlayerController:

We need to implement the method of registering this function to the event just now of mouseManager, which will call this function as soon as that event is enabled.

(Things with lightning indicate events)

 

To sum up, when we click on the ground, it will execute all the functions registered on the event and pass the parameters required by the function through Invoke:

   OnMouseClicked?.Invoke(hitInfo.point);
                //After clicking, pass the position to the function registered by the OnMouseClicked event and call it
            

So the next step is to realize the way the character moves when the mouse is clicked.

mouse pointer toggle

Import the picture and change it to Cursor

Select Point, then Apply again 

The second parameter is used to record the offset value, because it is necessary to make the center of the image have a click effect.

Note that the mouse size is too large, you can adjust it here:

Then you can adjust to achieve the effect of the mouse becoming a pointer.

camera follow

Here are three additional functions:

Move to view

Move your selected GameObject to the center of the Scene:

Align With View

This usage does not move the scene, but moves the selected object to the same center point as the scene

It is necessary to separate ordinary game objects and Camera.

Select a normal game object: move the center point of the object to the center point of the Scene

Select Camera: Make the Game perspective consistent with the Scene perspective

Align With View to Selected

This is to move the Scene, move the Scene to the center point of the selected object

Select the normal game object: move the center point of the scene to the center point of the object

Select Camera: Make the Scene perspective and Game perspective the same

Create cinemachine and tweak:

post-processing

Add fog to the distance, and the effect after adding fog is as follows:

Note to enable here:

Also enable post-processing in the rendering of the Main Camera

The effect after turning on bloom:

Then add ToneMapping, :mode ACES

Distortion effect:

Just move here

depth of field 

 

 FocalLength distance blur distance, Apeture blur degree

animation controller

Import the animation controller of the character here, use the fusion tree to fuse standing, walking and running, and then set the parameters in the script.

occlusion culling

 Create a shader and its corresponding material

  

Next, realize the effect that the character will have a rough outline behind the tree or other occluders.

Add a Finnier effect:

A color is needed here, so create color:

 

Open the shader and create a color

Next, I hope that this color and Frenier have a multiplication effect, so that the color can be added to the Frenier phenomenon,

Put the color up again

connect it

Hopefully this Fresnel effect can have some noise to make the effect look more realistic, so add some extra commonly used nodes.

Add dither, adjust the parameters to make it appear dense little dots, which can be used to fill the center of the ball 

Add an alpha for the occlusion threshold

 The final settings are as follows:

 

Next, go back to the game and apply this material when a character walks behind a tree, which involves URP

This is a render feature, which means that there will be some features that can be used when rendering. 

Next, add the player's layer to the player and select in the urp:

 This way you can achieve this effect when the character is behind the tree:

The effect is as follows:

But there will be problems when the character is in front of the camera, at this time we also add a render in the foreground

After adding it back to normal.

 Another problem here is that you cannot click behind the tree. Here you can select the tree as ignore Raycast.

Guess you like

Origin blog.csdn.net/weixin_43757333/article/details/122899840