Preface
The path finding system is a very commonly used function in games. To reach a certain point, the system needs to give a path. This can help the player plan the path and prompt the player to arrive. This function is widely used in various types of games. Applications
This case Navigation
uses Line Renderer
components to identify the indicator line while implementing the path finding system . The final effect picture is as follows:
Implementation process
1. Use Navigation to bake the scene
In order to realize the function of automatic path finding, first of all, you need to set up the basic settings of the scene so that the system can recognize which ones are the movable areas and which ones are not. The Navigation
navigation system's judgment conditions are based on static objects. If you want to judge dynamic objects, you need to add response components.
To set the object to static, first select the static ground and obstacles in the scene, select the inverted triangle of the Static option in the upper left corner of the Inspector panel of the Unity editor, and check the Navigation Static:
Next, enter the Navigation panel to perform the scene To bake, click on two Bake from top to bottom. The blue area in the figure is the movable area of the character, and the non-blue area is the non-crossable area, which is the related obstacle:
The specific details about baking can be found in the articles between:
2. Add response components to the role
In order to ensure that the role can be achieved wayfinding and indicator line features need to be added Nav Mesh Agent
and Line Renderer
two components:
First of all, for Nav Mesh Agent
the parameter adjustment, it is necessary to realize the path setting of the character. In this case, the parameter adjustment of the component is mainly in two aspects. The first is its speed. Setting the value can change the movement speed, and the other is the acceleration. , The size of the speed change when the target changes
The second component Line Renderer
is the focus of this case. Its main function is to draw lines in the three-dimensional scene. By using this feature, the path that the character will move can be drawn, so that the navigation function of the object can be realized.
About LIne Randerer
- The Line Renderer component accepts an array of two or more points in a three-dimensional space and draws a straight line between each point. You can use the line renderer to draw anything, from simple straight lines to complex spirals. The lines are always continuous; if you need to draw two or more completely independent lines, you should use multiple game objects, each with its own line renderer. The line renderer does not render pixel-wide lines. The polygons it renders have a width in world units. The line renderer uses the same line rendering algorithm as the Trail renderer.
In this case, after adding the component, we first need to add a material to it. You can set the number of materials in Materials, add materials to it, and adjust its width in width. The specific adjustments are shown in the figure:
3. Write a script to call two components to achieve navigation
Connect two components through scripts, first use their own methods to complete their functions, use Navigation
combined rays to detect collisions to complete the character movement to the clicked position, and the Line Renderer
components draw a line between the acquired series of points to complete the two The combination between the two is Navigation
to obtain a series of turning points during navigation through the designated function of the component, and assign them to Line Renderer
, thus completing the line drawing function:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class FindRode : MonoBehaviour
{
private NavMeshAgent agent;
private LineRenderer line;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
line = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,500))
{
if(hit.transform.tag=="ground")
{
agent.SetDestination(hit.point);
}
}
}
//如果导航里面的点大于1
if(agent.path.corners.Length>1)
{
//画线的点位等于导航的点位
line.positionCount = agent.path.corners.Length;
line.SetPositions(agent.path.corners);
}
}
}
The combination between the two is in the If judgment. We can agent.path.corners.Length
know the number of points that need to be drawn through the attributes, and through SetPositions()
the array that can get all the coordinates, we can draw between two adjacent points. line