目标是从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((中的curvedPointsIndex是0,所以如果有30个curvedLinePoints,它将从0开始计数到29,但我希望它显示在从1到30的最后一个WaypointText.text中。
我改为在Start((中尝试:
lastWaypointText.text = curvedPointsIndex.ToString();
制作
lastWaypointText.text = curvedPointsIndex + 1.ToString();
但这是使文本中的第一个数字为01,然后计数到29。
当您执行lastWaypointText.text = curvedPointsIndex + 1.ToString();
时,您只是将ToString应用于公式的最后一部分,使其添加";1〃;在您的最后一个WaypointIndex之后。你需要把它包装在一个括号里,而不是像这样:
lastWaypointText.text = (curvedPointsIndex + 1).ToString();