使用 ui 文本计算上次访问的航点时,如何使其从 1 而不是 0 开始计数?

用户1741587

目标是从 1 和 0 开始计数。

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

public class MoveOnCurvedLines : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;
    public float rotSpeed;
    public bool random = false;
    public int currentCurvedLinePointIndex;
    public Text lastWaypointText;

    private Vector3[] positions;
    private Vector3[] pos;
    private int index = 0;
    private bool goForward = true;
    private List<GameObject> curvedLinePoints = new List<GameObject>();
    private int numofposbetweenpoints;
    private bool getPositions = false;
    int randomIndex;
    int curvedPointsIndex;

    // Start is called before the first frame update
    void Start()
    {
        curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();

        if (curvedLinePoints != null && curvedLinePoints.Count > 0)
        {
            transform.rotation = curvedLinePoints[1].transform.rotation;
        }

        if (random)
            GetNewRandomIndex();

        lastWaypointText.text = curvedPointsIndex + 1.ToString();
    }

    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 (lineRenderer.positionCount > 0 && getPositions == false)
        {
            pos = GetLinePointsInWorldSpace();
            numofposbetweenpoints = pos.Length / curvedLinePoints.Count;

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

            getPositions = true;
        }

        if (go == true && lineRenderer.positionCount > 0)
        {
            Move();

            Vector3 targetDirection = (curvedLinePoints[c].transform.position - transform.position).normalized;
            curvedLinePoints[c].transform.localRotation = Quaternion.LookRotation(targetDirection);
            transform.localRotation = Quaternion.RotateTowards(transform.localRotation, curvedLinePoints[c].transform.localRotation, Time.deltaTime * rotSpeed);
        }

        var dist = Vector3.Distance(transform.position, curvedLinePoints[curvedPointsIndex].transform.position);
        if (dist < 0.1f)
        {
            if (curvedPointsIndex < curvedLinePoints.Count - 1)
            {
                curvedPointsIndex++;

                lastWaypointText.text = curvedPointsIndex.ToString();
            }

            currentCurvedLinePointIndex = curvedPointsIndex;
        }
    }

Start() 中的曲线点索引为 0,因此如果有 30 个曲线点,它将从 0 开始计数到 ​​29,但我希望它显示在 lastWaypointText.text 中,从 1 到 30。

我尝试使用 Start() 代替:

lastWaypointText.text = curvedPointsIndex.ToString();

lastWaypointText.text = curvedPointsIndex + 1.ToString();

但这会使文本中的第一个数字 01 然后数到 29。

艾丽高夫

当你这样做时,lastWaypointText.text = curvedPointsIndex + 1.ToString();你只是将 ToString 应用于公式的最后一部分,使其在你的 lastWaypointIndex 之后添加“1”。您需要将其包装在括号中,而不是像这样:

lastWaypointText.text = (curvedPointsIndex + 1).ToString();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 xsl:number 元素时从 0 开始计数而不是 1

如何让vim使用从1而不是0开始计数的相对行号

如何使tmux计数窗口从1而不是0开始?

forEach 索引从 1 而不是 0 开始计数?

如何从1而不是0开始排列键?

如何使tmux会话从1而不是0计数?

从1开始而不是列表的0开始计算索引

当索引不是1时如何从0开始数组

MSQL:如何获得0计数而不是1,并使用联接表或视图返回null

在c ++中从1而不是0开始计算字符串

使用范围和单元格时,数组似乎是从0开始而不是从1开始

indexpath.row从1而不是0开始

Terraform Range 函数从 1 而不是 0 开始

使用 numpy 和 matplotlib 时绘图不是从 0 开始

使用IIFE时,抛出“ TypeError`:`[0,1]`不是函数”

在servicestack中使用protobuf,为什么order只能从1开始而不是0?

如果计算机从0开始计数,为什么init进程的pid为1?

如何将 x 轴移动 1 以从 1 而不是 0 开始?

为什么我的迭代计数从 2 而不是 1 开始?

PHP:使用 count() 获得 1 而不是 0

为什么从列表[-1]而不是列表[0]开始计算此列表中的元素?(Python 3)

为什么javascript getMonth从0开始计数,getDate从1开始计数?

python:将numpy.array访问方法更改为从1而不是0开始

如何修改此真值表,使其使用并显示1和0,而不是true和false

从末尾索引列表时,为什么Python从索引-1(而不是0)开始?

如何将CollectionView单元格设置为从索引'1'而不是'0'开始-Swift

如何在Vega Lite中从1而不是0开始显示bar?

如何创建从页面 item1 而不是 item0 开始的底部导航栏

识别新值时从 1 开始计数