我正在尝试制作一个无尽的跑步游戏,其中玩家静止在平台上,并且对象以不同的模式在平台下方以初始速度实例化。我希望这些物体在达到一定的时间间隔后提高它们的速度。例如,如果时间间隔为 30 秒,基线速度为 10,我希望在 30 秒后速度达到 20,然后在再过 30 秒后增加到 30,等等。我有一个产卵器产卵在具有不同区域的重生点的障碍物模式中。
截至目前,障碍物无论如何都会以相同的速度生成,但一旦它们在时间间隔内生成,它们的速度就会增加。例如,生成对象 1,以初始速度移动 5 秒,然后增加速度,然后对象 2 在生成时执行与对象 1 完全相同的操作。这是我的对象移动代码:
using UnityEngine;
public class ObstacleMovement : MonoBehaviour
{
public Rigidbody rb;
// Keeps track of the time and increases speed based off the time
float time;
int seconds;
public int timeInterval; // Time interval where speed is increased
// Speed stuff
public Vector3 speedInc; // Speed increase each time interval
public float initialSpeed; // Initial speed in the Z direction
Vector3 currentSpeed; // Current speed of the object
// Vars for locking the object in place so no rotation/sliding occurs
float startPositionX;
Quaternion startRotation;
// Start is called before the first frame update
void Start()
{
startPositionX = transform.position.x;
startRotation = transform.rotation;
currentSpeed = new Vector3(0, 0, initialSpeed);
rb.velocity = currentSpeed;
}
// Update is called once per frame
void Update()
{
TrackTime();
if (seconds == timeInterval) {
currentSpeed = SpeedUp(currentSpeed);
time = 0;
}
rb.velocity = currentSpeed;
Debug.Log("Speed is: " + currentSpeed);
LockPos();
}
Vector3 SpeedUp(Vector3 iSpeed) {
Vector3 newSpeed = iSpeed + speedInc;
Debug.Log("Speeding up");
return newSpeed;
}
void TrackTime() {
time += Time.deltaTime;
seconds = (int)time % 60;
Debug.Log(seconds + " seconds have past");
}
void LockPos() {
Vector3 pos = transform.position;
pos.x = startPositionX;
transform.position = pos;
transform.rotation = startRotation;
}
}
在上面的代码中,我创建了一个计时器,跟踪秒数,然后在 XXX 秒过后提高速度。我究竟做错了什么?如果有必要,我可以发布生成器和生成点的代码。有没有更合适的地方来执行这个?谢谢!!