unity ——利用navemesh控制人物移动到目的地并切换动作

unity当中的navemesh组建可以建立导航网格,通过设置需要建立网格物体的属性为navigation static,在navigation面板中进行烘培,则可以在界面中看见可通行区域出现蓝色网格。可以在navigation面板中进行设置改变可以通行的区域。

给人物添加nav mesh agent 组件,可以进行相关的设置。

Property属性 Function功能
Agent Size代理大小
Radius半径

Radius of the agent, used to calculate collisions between obstacles and other agents.

代理半径,用于计算与其他代理和障碍物的碰撞

Height高度

The height clearance the agent needs to pass below an obstacle overhead.

代理需要通过头顶上方的障碍物的高度间隙。

Base offset

Offset of the collision cylinder in relation to the transform pivot point.

碰撞几何体相对于实际几何体垂直的偏移。

Steering操控
Speed速度

Maximum movement speed (in world units per second).

最大的移动速度(每秒)

Angular Speed角速度

Maximum speed of rotation (degrees per second).

最大旋转速度(每秒旋转的角度)

Acceleration

加速度

Maximum acceleration (in world units per second squared).

最大加速度

Stopping distance

停止距离

The agent will stop when this close to the goal location.

代理距离目标地的停止距离

Auto Braking

自动制动

When enabled, the agent will slow down when reaching the destination. You should disable this for behaviors such patrolling, where the agent should move smoothly between multiple points

当启用时,代理将在到达目的地时减慢速度。对于诸如巡逻之类的行为,您应该禁用此功能,因为代理应该在多个点之间平稳移动

Obstacle Avoidance障碍回避
Quality

Obstacle avoidance quality. If you have high number of agents you can save CPU time by reducing the obstacle avoidance quality. Setting avoidance to none, will only resolve collision, but will not try to actively avoid other agents and obstacles.

避障的质量。如果您有大量的代理,您可以通过降低避障质量来节省CPU时间。将“不避碰”设置为“不避碰”,只会解决碰撞问题,而不会主动避开其他因素和障碍。

Priority

Agents of lower priority will be ignored by this agent when performing avoidance. The value should be in the range 0–99 where lower numbers indicate higher priority.

在执行回避时,该代理将忽略低优先级的代理。该值应该在0-99范围内,其中数字越低表示优先级越高。

Path Finding寻路
Auto Traverse OffMesh Link

Set to true to automatically traverse off-mesh links. You should turn this off when you you want to use animation or some specific way to traverse off-mesh links.

设置为true以自动遍历非网格链接。当您想要使用动画或一些特定的方法来遍历非网格链接时,您应该关闭此功能。

Auto Repath

When enabled the agent will try to find path again when it reaches the end of a partial path. When there is no path to the destination, a partial path is generated to the closest reachable location to the destination.

当启用代理时,当它到达部分路径的末尾时,代理将再次尝试查找路径。当没有到目的地的路径时,会生成到离目的地最近的可到达位置的部分路径。

Area Mask

Area mask describes which area types the agent will consider when finding a path. When you prepare meshes for NavMesh baking, you can set each meshes area type. For example you can mark stairs with special area type, and forbid some character types from using the stairs.

局部蒙片描述代理在查找路径时将考虑哪些区域类型。当你准备网格NavMesh烘焙,你可以设置每个网格面积类型。例如,可以用特殊的区域类型标记楼梯,并禁止某些字符类型使用楼梯。

在终点添加一物体,为人物添加脚本,在初始函数中添加agent.destination = goal.position;则人物可以自行寻找到终点并规划路径。也可以设置终点位置des,利用agent.Setdesnation(des);寻找终点。

人物的动画调用,可以通过判断人物的位置和终点位置之间的距离进行切换。

代码如下所示:

public class move : MonoBehaviour {

    public Transform goal;
    private NavMeshAgent agent;
    private Animation ani;
    // Use this for initialization
    void Start ()
    {
        ani = GetComponent<Animation>();
        ani.Play();
        ani.wrapMode = WrapMode.Loop;
        agent = GetComponent<NavMeshAgent>();
        agent.destination = goal.position;
    }
	
	// Update is called once per frame
	void Update ()
    {
        //播放动画,判断是否到达了目的地,播放空闲动画  
        if ((agent.transform.position - goal.transform.position).magnitude < 1)
        {
            ani.Play("idle");
            //到达目的地不再移动位置
            agent.destination = agent.transform.position;
        }

    }
}

以上仅供参考,望指正。

猜你喜欢

转载自blog.csdn.net/weixin_41340063/article/details/81810816