Unity3D - 路径的编辑与跟随路径的移动

本文介绍如何在Unity3D中实现AI角色跟随预设路径移动,并可视化路径编辑。通过挂载脚本到游戏物体,创建多个路径点,实现物体沿路径平滑移动。同时提供代码示例,包括路径编辑和跟随路径移动两部分,允许动态调整路径并循环运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unity3D跟随路径移动

在做项目的时候,突然想到要做一个可以跟随路径走的AI,而且路径可视化。
原视频地址:https://www.youtube.com/watch?v=1aBjTa3xQzE

分为两部分代码,第一个是编辑路径的代码,另一个为跟随路径的代码。


代码如下

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

public class EditorPathScript : MonoBehaviour {

    public Color rayColor = Color.white;
    public List<Transform> path_objs = new List<Transform>();
    Transform[] theArray;

    void OnDrawGizmos(){
        Gizmos.color = rayColor;
        theArray = GetComponentsInChildren<Transform>();
        path_objs.Clear();
        foreach(Transform path_obj in theArray){
            if(path_obj != this.transform){
                path_objs.Add(pat
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值