我遇到以下代码时出现问题:
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
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;
public void Start()
{
if (Path == null)
{
Debug.LogError("Path cannot be null", gameObject);
return;
}
_currentPoint = Path.GetPathEnumerator();
_currentPoint.MoveNext();
if (_currentPoint.Current == null)
return;
transform.position = _currentPoint.Current.position;
}
public void Update()
{
if (_currentPoint == null || _currentPoint.Current == null)
return;
if (Type == FollowType.MoveTowards)
transform.position = Vector3.MoveTowards (transform.position,
_currentPoint.Current.position, Time.deltaTime * Speed);
else if (Type == FollowType.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();
}
}
以下是我遇到的错误:
Assets / Code / FollowPath.cs(28,36):错误CS1061:输入
PathDefinition' does not contain a definition for
GetPathEnumerator&#39;并且没有扩展方法GetPathEnumerator' of type
PathDefinition&#39;可以找到(你错过了使用指令或程序集引用吗?)
答案 0 :(得分:0)
此缺失的代码似乎来自3DBuzz YouTube视频Creating 2D Games in Unity 4.5 #4 - Moving Platforms。用户26dollar转录了代码,我在这里自动格式化并重现:
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class PathDefinition : MonoBehaviour
{
public Transform[] Points;
public IEnumerator<Transform> GetPathEnumerator()
{
if (Points == null || Points.Length < 1)
yield break;
var direction = 1;
var 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()
{
if (Points == null || Points.Length < 2)
return;
for (var i = 1; i < Points.Length; i++) {
Gizmos.DrawLine(Points[i - 1].position, Points[i].position);
}
}
}