Unity 实现2D游戏中物体延自定义路径反复移动

Unity版本为5.3.2

这里写图片描述

首先随便新建一个2D游戏场景

这里写图片描述

在场景中新建一个空物体名字为Path,再创建三个子物体分别为Start,Mid,End,改变icon图标(容易设计)

首先实现路径点间的画线

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
public class PathDefinition : MonoBehaviour {

    public Transform[] points;//路径点集合

    public IEnumerator<Transform> GetPathsEnumerator(){//自定义迭代器(用来返回路径点)
        if (points == null || points.Length < 1)
            yield break;
        var direction = 1;//移动方向
        int index = 0;
        while (true) {
            yield return points [index];
            if (points.Length == 1)
                continue;
            if (index <= 0)
                direction = 1;
            else if (index >= points.Length - 1)
                direction = -1;
            index = index + direction;
        }
    }

    public void OnDrawGizmos(){//重写OnDrawGizmos实现在scene视图中画线
        if (points==null||points.Length < 2)
            return;
        var pt = points.Where (t=>t!=null).ToList();//使用的Linq语言集成查询,找出路径点集合中不为空的点
        if (pt.Count < 2)
            return;
        for (int i = 1; i < pt.Count; i++) {
            Gizmos.DrawLine (pt[i-1].position,pt[i].position);//画线
        }
    }
}

实现移动

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FollowPath : MonoBehaviour {

    public enum FollowType{//定义一个移动类型的枚举
        MoveTowards,
        Lerp
    }

    public FollowType type = FollowType.MoveTowards;//默认为直接朝向移动
    public PathDefinition path;
    public float speed = 1;//移动速度
    public float maxDistanceTOGoal = .1f;//判断物体是否到达某个路径点

    private IEnumerator<Transform> _currentPoint;//声明一个迭代器

    void Start(){
        if (path == null) {
            Debug.Log ("Path cannot be null",gameObject);
            return;
        }
        _currentPoint = path.GetPathsEnumerator ();//获得迭代器实例
        _currentPoint.MoveNext ();//MoveNext ()会执行代码到迭代器中yiled 语句处停止(包括yiled 语句)
        if (_currentPoint.Current == null)//Current代表迭代器当前返回值
            return;
        transform.position = _currentPoint.Current.position;//默认物体初始位置为路径点的第一个位置
    }

    void Update(){
        if (_currentPoint == null || _currentPoint.Current == null)
            return;
        if (type == FollowType.MoveTowards) {//使用Vector3.MoveTowards移动
            transform.position = Vector3.MoveTowards (transform.position, _currentPoint.Current.position, Time.deltaTime * speed);
        }
        else if(type==FollowType.Lerp){//使用Vector3.Lerp移动
            transform.position = Vector3.Lerp(transform.position,_currentPoint.Current.position,Time.deltaTime*speed);
        }
        var distanceSquared = (transform.position - _currentPoint.Current.position).sqrMagnitude;
        if (distanceSquared < maxDistanceTOGoal * maxDistanceTOGoal)//如果物体与某个路径点的距离的平方小于规定的最大值,则获取下一个路径点
            _currentPoint.MoveNext ();
    }
}

这里写图片描述

在Path上挂上PathDefinition脚本,将三个路径点挂上去,在Move物体上挂上FollowPath脚本,将Path拖到FollowPath上,然后运行

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值