Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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# 如何在轮到每个对象时使用时间移动每个对象?_C#_Unity3d - Fatal编程技术网

C# 如何在轮到每个对象时使用时间移动每个对象?

C# 如何在轮到每个对象时使用时间移动每个对象?,c#,unity3d,C#,Unity3d,目标是使用时间变量,例如,如果变量int值为5,则每5秒另一个对象将开始在位置上移动 using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class MoveOnCurvedLines : MonoBehaviour { public LineRenderer lineRenderer; pub

目标是使用时间变量,例如,如果变量int值为5,则每5秒另一个对象将开始在位置上移动

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;

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

        pos = GetLinePointsInWorldSpace();

        if (moveToFirstPositionOnStart == true)
        {
            objectsToMove[0].transform.position = pos[index];
        }
    }

    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 < objectsToMove.Count; i++)
        {
            Vector3 newPos = objectsToMove[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;
                }
            }

            objectsToMove[i].transform.position = newPos;
        }
    }
}
现在,使用Move方法中的循环:

for (int i = 0; i < objectsToMove.Count; i++)
for(int i=0;i
所有的对象都在同时移动,但是我想做一些队列逻辑,这样每N秒另一个对象就会开始在位置上移动

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;

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

        pos = GetLinePointsInWorldSpace();

        if (moveToFirstPositionOnStart == true)
        {
            objectsToMove[0].transform.position = pos[index];
        }
    }

    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 < objectsToMove.Count; i++)
        {
            Vector3 newPos = objectsToMove[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;
                }
            }

            objectsToMove[i].transform.position = newPos;
        }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用UnityEngine;
公共类移动曲线:单一行为
{
公共线条渲染器线条渲染器;
public List objectsToMove=new List();
公众浮标速度;
公共布尔go=假;
public bool moveToFirstPositionOnStart=false;
私有向量3[]个位置;
私有向量3[]位;
私有整数指数=0;
private bool goForward=true;
//在第一帧更新之前调用Start
void Start()
{
objectsToMove=GameObject.FindGameObjectsWithTag(“新预置”).ToList();
pos=GetLinePointsInWorldSpace();
if(moveToFirstPositionOnStart==true)
{
objectsToMove[0].transform.position=pos[index];
}
}
Vector3[]GetLinePointsInWorldSpace()
{
位置=新矢量3[lineRenderer.positionCount];
//获取检查器中显示的位置
lineRenderer.GetPositions(位置);
//返回的点位于世界空间中
返回位置;
}
//每帧调用一次更新
无效更新()
{
如果(go==真)
{
Move();
}
}
无效移动()
{
for(int i=0;i=位置长度-1;
如果(!atLastOne)索引++;
else{index--;goForward=false;}
}
其他的
{//倒退:
bool atFirstOne=索引=位置长度-1;
如果(!atLastOne)索引++;
else{index--;goForward=false;}
}
其他的
{//倒退:
bool-atFirstOne=index
inti=0;
void StartMove()
{
i=0;
调用重复(nameof(Move),3f,0.1f);
}
无效移动()
{
if(i=位置长度-1;
如果(!atLastOne)索引++;
else{index--;goForward=false;}
}
其他的
{//倒退:

bool atFirstOne=index您是否尝试过调用重复()
?这可能就是您正在寻找的解决方案。@Ankit我现在尝试了开始调用重复(“Move”,3f,0.1f);而不是调用Move()在更新中。但是预制件没有移动,或者移动速度太慢,并且不确定它们是否按照我的要求一个接一个地移动。我没有在移动方法中更改任何内容。你能更新你的问题代码吗?@Ankit用我尝试过的代码更新了我的问题。让我更新一下你的代码。对象根本没有移动。我尝试调用StartMove from inside Start()像这样调用重复(“Move”,0.5f,0.1f);也尝试过调用重复(“Move”,3f,0.1f);或者调用重复(“Move”,0.5f,3f);但是对象根本没有移动。我也像你一样尝试过调用重复(nameof(Move),3f,0.1f);但是没有任何变化。您能告诉我您的原始代码的输出/结果是什么吗?很抱歉,我有点忙。
int i = 0;
void StartMove()
{
    i = 0;
    InvokeRepeating(nameof(Move), 3f, 0.1f);
}
void Move()
{
    if( i < objectsToMove.Count)
    {
        Vector3 newPos = objectsToMove[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;
            }
        }

        objectsToMove[i].transform.position = newPos;
        i++;
    }
}