Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么要移动的对象彼此移动,而不是以3秒的空间延迟彼此跟随? 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用System.Linq; 使用UnityEngine; 公共类移动曲线:单一行为 { 公共线条渲染器线条渲染器; public List objectsToMove=new List(); 公众浮标速度; 公共布尔go=假; public bool moveToFirstPositionOnStart=false; 私有向量3[]个位置; 私有向量3[]位; 私有整数指数=0; private bool goForward=true; 私有列表objectsToMoveCopy=new List(); //在第一帧更新之前调用Start void Start() { objectsToMove=GameObject.FindGameObjectsWithTag(“新预置”).ToList(); pos=GetLinePointsInWorldSpace(); if(moveToFirstPositionOnStart==true) { for(int i=0;igo.GetComponent().ToList(); foreach(移动对象中的移动曲线对象) { 初始目标(位置、速度、移动到起始位置); } Start例程(TriggerObjects(false)); } 向量3[]获取位置() { Vector3[]位置=新Vector3[lineRenderer.positionCount]; //获取检查器中显示的位置 lineRenderer.GetPositions(位置); 返回位置; } IEnumerator触发器对象(布尔延迟第一对象) { WaitForSeconds waitThreeSeconds=新的WaitForSeconds(3); if(delayFirstObject) 返回等待三秒钟; foreach(移动对象中的移动曲线对象) { obj.StartMoving(); 返回等待三秒钟; } } }_C#_Unity3d - Fatal编程技术网

C# 为什么要移动的对象彼此移动,而不是以3秒的空间延迟彼此跟随? 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用System.Linq; 使用UnityEngine; 公共类移动曲线:单一行为 { 公共线条渲染器线条渲染器; public List objectsToMove=new List(); 公众浮标速度; 公共布尔go=假; public bool moveToFirstPositionOnStart=false; 私有向量3[]个位置; 私有向量3[]位; 私有整数指数=0; private bool goForward=true; 私有列表objectsToMoveCopy=new List(); //在第一帧更新之前调用Start void Start() { objectsToMove=GameObject.FindGameObjectsWithTag(“新预置”).ToList(); pos=GetLinePointsInWorldSpace(); if(moveToFirstPositionOnStart==true) { for(int i=0;igo.GetComponent().ToList(); foreach(移动对象中的移动曲线对象) { 初始目标(位置、速度、移动到起始位置); } Start例程(TriggerObjects(false)); } 向量3[]获取位置() { Vector3[]位置=新Vector3[lineRenderer.positionCount]; //获取检查器中显示的位置 lineRenderer.GetPositions(位置); 返回位置; } IEnumerator触发器对象(布尔延迟第一对象) { WaitForSeconds waitThreeSeconds=新的WaitForSeconds(3); if(delayFirstObject) 返回等待三秒钟; foreach(移动对象中的移动曲线对象) { obj.StartMoving(); 返回等待三秒钟; } } }

C# 为什么要移动的对象彼此移动,而不是以3秒的空间延迟彼此跟随? 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用System.Linq; 使用UnityEngine; 公共类移动曲线:单一行为 { 公共线条渲染器线条渲染器; public List objectsToMove=new List(); 公众浮标速度; 公共布尔go=假; public bool moveToFirstPositionOnStart=false; 私有向量3[]个位置; 私有向量3[]位; 私有整数指数=0; private bool goForward=true; 私有列表objectsToMoveCopy=new List(); //在第一帧更新之前调用Start void Start() { objectsToMove=GameObject.FindGameObjectsWithTag(“新预置”).ToList(); pos=GetLinePointsInWorldSpace(); if(moveToFirstPositionOnStart==true) { for(int i=0;igo.GetComponent().ToList(); foreach(移动对象中的移动曲线对象) { 初始目标(位置、速度、移动到起始位置); } Start例程(TriggerObjects(false)); } 向量3[]获取位置() { Vector3[]位置=新Vector3[lineRenderer.positionCount]; //获取检查器中显示的位置 lineRenderer.GetPositions(位置); 返回位置; } IEnumerator触发器对象(布尔延迟第一对象) { WaitForSeconds waitThreeSeconds=新的WaitForSeconds(3); if(delayFirstObject) 返回等待三秒钟; foreach(移动对象中的移动曲线对象) { obj.StartMoving(); 返回等待三秒钟; } } },c#,unity3d,C#,Unity3d,对象合并,因为对象的目标位置只有一个索引,这意味着所有对象都朝着同一点移动,而不是每个对象朝着路径上各自的下一点移动。这会在第一个对象掉头并向后运行时导致合并 最好将逻辑分为两个类,否则您必须分别跟踪每个对象的路径,这意味着您需要为每个对象的当前目标位置设置一个int[]index,为goForwardbools设置另一个数组,等等,为您引入的每个新属性设置一个数组 控制器: using System; using System.Collections; using System.Collect

对象合并,因为对象的目标位置只有一个索引,这意味着所有对象都朝着同一点移动,而不是每个对象朝着路径上各自的下一点移动。这会在第一个对象掉头并向后运行时导致合并

最好将逻辑分为两个类,否则您必须分别跟踪每个对象的路径,这意味着您需要为每个对象的当前目标位置设置一个
int[]index
,为
goForward
bools设置另一个数组,等等,为您引入的每个新属性设置一个数组

控制器:

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

public class MoveOnCurvedLines : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public List<GameObject> objectsToMove = new List<GameObject>();
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;

    private Vector3[] positions;
    private Vector3[] pos;
    private int index = 0;
    private bool goForward = true;
    private List<GameObject> objectsToMoveCopy = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        objectsToMove = GameObject.FindGameObjectsWithTag("New Prefab").ToList();

        pos = GetLinePointsInWorldSpace();

        if (moveToFirstPositionOnStart == true)
        {
            for (int i = 0; i < objectsToMove.Count; i++)
            {
                objectsToMove[i].transform.position = pos[index];
            }
        }

        StartCoroutine(AddNew());
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        positions = new Vector3[lineRenderer.positionCount];
        //Get the positions which are shown in the inspector 
        lineRenderer.GetPositions(positions);


        //the points returned are in world space
        return positions;
    }

    // Update is called once per frame
    void Update()
    {
        if (go == true)
        {
            Move();
        }
    }

    void Move()
    {
        for (int i = 0; i < objectsToMoveCopy.Count; i++)
        {
            Vector3 newPos = objectsToMoveCopy[i].transform.position;
            float distanceToTravel = speed * Time.deltaTime;

            bool stillTraveling = true;
            while (stillTraveling)
            {
                Vector3 oldPos = newPos;
                newPos = Vector3.MoveTowards(oldPos, pos[index], distanceToTravel);
                distanceToTravel -= Vector3.Distance(newPos, oldPos);
                if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
                {
                    // when you hit a waypoint:
                    if (goForward)
                    {
                        bool atLastOne = index >= pos.Length - 1;
                        if (!atLastOne) index++;
                        else { index--; goForward = false; }
                    }
                    else
                    { // going backwards:
                        bool atFirstOne = index <= 0;
                        if (!atFirstOne) index--;
                        else { index++; goForward = true; }
                    }
                }
                else
                {
                    stillTraveling = false;
                }
            }

            objectsToMoveCopy[i].transform.position = newPos;
        }
    }

    IEnumerator AddNew()
    {
        WaitForSeconds waitThreeSeconds = new WaitForSeconds(3);

        foreach (var objToMove in objectsToMove)
        {
            yield return waitThreeSeconds;
            objectsToMoveCopy.Add(objToMove);
        }
    }
}
公共类MovementController:MonoBehavior
{
[序列化字段]
专用线条渲染器线条渲染器;
[序列化字段]
私人浮动速度;
[序列化字段]
私人布尔移动到起始位置;
public List movingObjects=new List();
void Start()
{
Vector3[]位置=GetPositions();
movingObjects=GameObject.FindGameObjectsSwithTag(“新预置”).Select(go=>go.GetComponent().ToList();
foreach(移动对象中的移动曲线对象)
{
初始目标(位置、速度、移动到起始位置);
}
Start例程(TriggerObjects(false));
}
向量3[]获取位置()
{
Vector3[]位置=新Vector3[lineRenderer.positionCount];
//获取检查器中显示的位置
lineRenderer.GetPositions(位置);
返回位置;
}
IEnumerator触发器对象(布尔延迟第一对象)
{
WaitForSeconds waitThreeSeconds=新的WaitForSeconds(3);
if(delayFirstObject)
返回等待三秒钟;
foreach(移动对象中的移动曲线对象)
{
obj.StartMoving();
返回等待三秒钟;
}
}
}
运动逻辑:

public class MovementController : MonoBehaviour
{
    [SerializeField]
    private LineRenderer lineRenderer;
    [SerializeField]
    private float speed;
    [SerializeField]
    private bool moveToFirstPositionOnStart;

    public List<MoveOnCurvedLines> movingObjects = new List<MoveOnCurvedLines>();

    void Start()
    {
        Vector3[] positions = GetPositions();

        movingObjects = GameObject.FindGameObjectsWithTag("New Prefab").Select(go => go.GetComponent<MoveOnCurvedLines>().ToList();

        foreach (MoveOnCurvedLines obj in movingObjects)
        {
            obj.Init(positions, speed, moveToFirstPositionOnStart);
        }

        StartCoroutine(TriggerObjects(false));
    }

    Vector3[] GetPositions()
    {
        Vector3[] positions = new Vector3[lineRenderer.positionCount];
        //Get the positions which are shown in the inspector 
        lineRenderer.GetPositions(positions);
        return positions;
    }

    IEnumerator TriggerObjects(bool delayFirstObject)
    {
        WaitForSeconds waitThreeSeconds = new WaitForSeconds(3);

        if (delayFirstObject)
            yield return waitThreeSeconds;

        foreach (MoveOnCurvedLines obj in movingObjects)
        {
            obj.StartMoving();
            yield return waitThreeSeconds;
        }
    }
}
公共类MoveOnCurvedLines:单行为
{
私有变换;
私有布尔初始化;
私有向量3[]位;
私有int posIndex=0;
私人浮动速度;
private bool goForward=true;
私有协同路由移动;
public void Init(向量3[]位置、浮动速度、bool instantlyMoveToFirstPosition)
{
myTransform=转换;
pos=位置;
速度=速度;
if(instantlyMoveToFirstPosition)
myTransform.position=位置[0];
初始化=真;
}
公共无效开始移动()
{
if(初始化和移动==null)
moving=start例程(Move());
}
公共空间停止移动()
{
如果(正在移动!=null)
{
StopCorroutine(移动);
移动=空;
}
}
私有IEnumerator移动()
{
while(true)
{
Vector3 newPos=myTransform.position;
浮动距离到行程=速度*时间.deltaTime;
bool=true;
当(仍在旅行)
{
向量3 oldPos=newPos;
newPos=Vector3.向(oldPos,pos[posIndex],distanceToTravel)移动;
distanceToTravel-=矢量3.距离(新位置、旧位置);
如果(newPos==pos[posIndex])///Vector3比较是近似值,那么这是确定的
{
//当您到达一个航路点时:
如果(前进)
public class MoveOnCurvedLines : MonoBehaviour
{
    private Transform myTransform;
    private bool initialized;

    private Vector3[] pos;
    private int posIndex = 0;
    private float speed;
    private bool goForward = true;
    private Coroutine moving;

    public void Init(Vector3[] positions, float speed, bool instantlyMoveToFirstPosition)
    {
        myTransform = transform;
        pos = positions;
        this.speed = speed;
        if (instantlyMoveToFirstPosition)
            myTransform.position = positions[0];
        initialized = true;
    }

    public void StartMoving()
    {
        if (initialized && moving == null)
            moving = StartCoroutine(Move());
    }

    public void StopMoving()
    {
        if (moving != null)
        {
            StopCoroutine(moving);
            moving = null;
        }
    }

    private IEnumerator Move()
    {
        while (true)
        {
            Vector3 newPos = myTransform.position;
            float distanceToTravel = speed * Time.deltaTime;

            bool stillTraveling = true;
            while (stillTraveling)
            {
                Vector3 oldPos = newPos;
                newPos = Vector3.MoveTowards(oldPos, pos[posIndex], distanceToTravel);
                distanceToTravel -= Vector3.Distance(newPos, oldPos);
                if (newPos == pos[posIndex]) // Vector3 comparison is approximate so this is ok
                {
                    // when you hit a waypoint:
                    if (goForward)
                    {
                        bool atLastOne = posIndex >= pos.Length - 1;
                        if (!atLastOne)
                        {
                            posIndex++;
                        }
                        else
                        {
                            posIndex--;
                            goForward = false;
                        }
                    }
                    else
                    { // going backwards:
                        bool atFirstOne = posIndex <= 0;
                        if (!atFirstOne)
                        {
                            posIndex--;
                        }
                        else
                        {
                            posIndex++; 
                            goForward = true;
                        }
                    }
                }
                else
                {
                    stillTraveling = false;
                }
            }

            myTransform.position = newPos;
        }
    }
}