Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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# 如果goForward状态在运行时或开始时发生更改,如何更改向前或向后移动转换的代码? 使用系统集合; 使用System.Collections.Generic; 使用System.Linq; 使用UnityEngine; 公共类航路点如下:单行为 { 公众浮标速度; 公共航路点航路点; 公共图书馆; 公共事业发展; 私有整数指数=0; 专用整数计数器=0; 私有int c=0; 私有列表curvedLinePoints=新列表(); 公共int-numofpos点与点之间; 私人布尔盖特恩斯; 私有void Start() { waypoints=GameObject.Find(“waypoints”).GetComponent(); curvedLinePoints=GameObject.FindGameObjectsWithTag(“曲线点”).ToList(); } 私有void更新() { if(getonce==false) { numofposbetweenpoints=曲线线点。计数; getonce=true; } if(go==true&&waypoints.linerenderpositions.Count>0) { Move(); } } 私人空位移动() { Vector3 newPos=变换位置; 浮动距离到行程=速度*时间.deltaTime; bool=true; 当(仍在旅行) { 向量3 oldPos=newPos; //错误异常超出第55行的界限,请检查!!!!! newPos=Vector3.向(oldPos、航路点、linerenderpositions[索引]、distanceToTravel)移动; distanceToTravel-=矢量3.距离(新位置、旧位置); 如果(newPos==waypoints.lineRenderPositions[index])///Vector3比较是近似值,那么这是确定的 { //当您到达一个航路点时: 如果(前进) { 布尔atLastOne=索引>=航路点.LineRenderPositions.Count-1; 如果(!atLastOne) { 索引++; 计数器++; if(计数器==numoffosbetweenpoints) { C++; 计数器=0; } if(c==curvedLinePoints.Count-1) { c=0; } } else{index--;goForward=false;} } 其他的 {//倒退: bool atFirstOne=索引_C#_Unity3d - Fatal编程技术网

C# 如果goForward状态在运行时或开始时发生更改,如何更改向前或向后移动转换的代码? 使用系统集合; 使用System.Collections.Generic; 使用System.Linq; 使用UnityEngine; 公共类航路点如下:单行为 { 公众浮标速度; 公共航路点航路点; 公共图书馆; 公共事业发展; 私有整数指数=0; 专用整数计数器=0; 私有int c=0; 私有列表curvedLinePoints=新列表(); 公共int-numofpos点与点之间; 私人布尔盖特恩斯; 私有void Start() { waypoints=GameObject.Find(“waypoints”).GetComponent(); curvedLinePoints=GameObject.FindGameObjectsWithTag(“曲线点”).ToList(); } 私有void更新() { if(getonce==false) { numofposbetweenpoints=曲线线点。计数; getonce=true; } if(go==true&&waypoints.linerenderpositions.Count>0) { Move(); } } 私人空位移动() { Vector3 newPos=变换位置; 浮动距离到行程=速度*时间.deltaTime; bool=true; 当(仍在旅行) { 向量3 oldPos=newPos; //错误异常超出第55行的界限,请检查!!!!! newPos=Vector3.向(oldPos、航路点、linerenderpositions[索引]、distanceToTravel)移动; distanceToTravel-=矢量3.距离(新位置、旧位置); 如果(newPos==waypoints.lineRenderPositions[index])///Vector3比较是近似值,那么这是确定的 { //当您到达一个航路点时: 如果(前进) { 布尔atLastOne=索引>=航路点.LineRenderPositions.Count-1; 如果(!atLastOne) { 索引++; 计数器++; if(计数器==numoffosbetweenpoints) { C++; 计数器=0; } if(c==curvedLinePoints.Count-1) { c=0; } } else{index--;goForward=false;} } 其他的 {//倒退: bool atFirstOne=索引

C# 如果goForward状态在运行时或开始时发生更改,如何更改向前或向后移动转换的代码? 使用系统集合; 使用System.Collections.Generic; 使用System.Linq; 使用UnityEngine; 公共类航路点如下:单行为 { 公众浮标速度; 公共航路点航路点; 公共图书馆; 公共事业发展; 私有整数指数=0; 专用整数计数器=0; 私有int c=0; 私有列表curvedLinePoints=新列表(); 公共int-numofpos点与点之间; 私人布尔盖特恩斯; 私有void Start() { waypoints=GameObject.Find(“waypoints”).GetComponent(); curvedLinePoints=GameObject.FindGameObjectsWithTag(“曲线点”).ToList(); } 私有void更新() { if(getonce==false) { numofposbetweenpoints=曲线线点。计数; getonce=true; } if(go==true&&waypoints.linerenderpositions.Count>0) { Move(); } } 私人空位移动() { Vector3 newPos=变换位置; 浮动距离到行程=速度*时间.deltaTime; bool=true; 当(仍在旅行) { 向量3 oldPos=newPos; //错误异常超出第55行的界限,请检查!!!!! newPos=Vector3.向(oldPos、航路点、linerenderpositions[索引]、distanceToTravel)移动; distanceToTravel-=矢量3.距离(新位置、旧位置); 如果(newPos==waypoints.lineRenderPositions[index])///Vector3比较是近似值,那么这是确定的 { //当您到达一个航路点时: 如果(前进) { 布尔atLastOne=索引>=航路点.LineRenderPositions.Count-1; 如果(!atLastOne) { 索引++; 计数器++; if(计数器==numoffosbetweenpoints) { C++; 计数器=0; } if(c==curvedLinePoints.Count-1) { c=0; } } else{index--;goForward=false;} } 其他的 {//倒退: bool atFirstOne=索引,c#,unity3d,C#,Unity3d,如果goForward状态在运行时或开始时发生更改,如何更改向前或向后移动转换的代码 考虑使用数学的%(模)运算符。此运算符执行“时钟数学”。将其视为时钟上的指针,当指针到达时钟上的12时,指针“环绕”开始并以1开始。以数字4为例。如果我们有一个类似 for(int i=0;i

如果goForward状态在运行时或开始时发生更改,如何更改向前或向后移动转换的代码

考虑使用数学的
%
(模)运算符。此运算符执行“时钟数学”。将其视为时钟上的指针,当指针到达时钟上的12时,指针“环绕”开始并以1开始。以数字4为例。如果我们有一个类似

for(int i=0;i<8;i++)
{
WriteLine($“{i%4}”);
}
它将产生0,1,2,3,0,1,2,3。这实际上给了你一个围绕数字4的循环索引

如果我们在数组(或列表)的上下文中查看这一点,我们可以前后循环一个数组,而无需检查是否位于数组(或列表)的第一个或最后一个位置

这种效果非常有助于在任意航路点之间旅行,避免令人讨厌的
索引自动边界感知

您可以实现类似这样的功能以达到所需的效果:

private void Move()
{
Vector3 newPos=变换位置;
浮动距离到行程=速度*时间.deltaTime;
bool=true;
当(仍在旅行)
{
向量3 oldPos=newPos;
//错误异常超出第55行的界限,请检查!!!!!
newPos=Vector3.向(oldPos、航路点、linerenderpositions[索引]、distanceToTravel)移动;
distanceToTravel-=矢量3.距离(新位置、旧位置);
如果(newPos==waypoints.lineRenderPositions[index])///Vector3比较是近似值,那么这是确定的
{
//确定我们应该向前还是向后浏览列表
int方向=goForward?1:-1;
//循环索引,如果我们继续并到达列表的末尾,它会自动换行为0,
//或者,如果我们倒退到列表的末尾
索引=(索引+方向)%waypoints.LineRenderPositions.Count;
//增加我们通过的点数
计数器++;
if(计数器==numoffosbetweenpoints)
{
C++;
计数器=0;
}
//如果我们到达终点,重置C
if(c==curvedLinePoints.Count-1)
{
c=0;
}
}
其他的
{
仍然旅行=假;
}
}
transform.position=newPos;
}
我建议阅读。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class WaypointsFollower : MonoBehaviour
{
    public float speed;
    public Waypoints waypoints;
    public bool go;
    public bool goForward;

    private int index = 0;
    private int counter = 0;
    private int c = 0;
    private List<GameObject> curvedLinePoints = new List<GameObject>();

    public int numofposbetweenpoints;

    private bool getonce;

    private void Start()
    {
        waypoints = GameObject.Find("Waypoints").GetComponent<Waypoints>();

        curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();
    }

    private void Update()
    {
        if (getonce == false)
        {
            numofposbetweenpoints = curvedLinePoints.Count;

            getonce = true;
        }

        if (go == true && waypoints.lineRendererPositions.Count > 0)
        {
            Move();
        }
    }

    private void Move()
    {
        Vector3 newPos = transform.position;
        float distanceToTravel = speed * Time.deltaTime;

        bool stillTraveling = true;
        while (stillTraveling)
        {
            Vector3 oldPos = newPos;

            // error exception out of bound on line 55 to check !!!!!
            newPos = Vector3.MoveTowards(oldPos, waypoints.lineRendererPositions[index], distanceToTravel);

            distanceToTravel -= Vector3.Distance(newPos, oldPos);
            if (newPos == waypoints.lineRendererPositions[index]) // Vector3 comparison is approximate so this is ok
            {
                // when you hit a waypoint:
                if (goForward)
                {
                    bool atLastOne = index >= waypoints.lineRendererPositions.Count - 1;
                    if (!atLastOne)
                    {
                        index++;
                        counter++;
                        if (counter == numofposbetweenpoints)
                        {
                            c++;

                            counter = 0;
                        }
                        if (c == curvedLinePoints.Count - 1)
                        {
                            c = 0;
                        }
                    }
                    else { index--; goForward = false; }
                }
                else
                { // going backwards:
                    bool atFirstOne = index <= 0;
                    if (!atFirstOne)
                    {
                        index--;

                        counter++;
                        if (counter == numofposbetweenpoints)
                        {
                            c++;

                            counter = 0;
                        }
                        if (c == curvedLinePoints.Count - 1)
                        {
                            c = 0;
                        }
                    }
                    else { index++; goForward = true; }
                }
            }
            else
            {
                stillTraveling = false;
            }
        }

        transform.position = newPos;
    }
}