首页 > 解决方案 > 如何提高实例化对象的生成速度?

问题描述

我正在尝试制作一个无尽的跑步游戏,其中玩家静止在平台上,并且对象以不同的模式在平台下方以初始速度实例化。我希望这些物体在达到一定的时间间隔后提高它们的速度。例如,如果时间间隔为 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 秒过后提高速度。我究竟做错了什么?如果有必要,我可以发布生成器和生成点的代码。有没有更合适的地方来执行这个?谢谢!!

标签: unity3d

解决方案


如果我理解正确的话,问题是你新生成的障碍物没有“游戏当前速度”,对吧?

发生这种情况是因为您正在控制与游戏状态分开的每个障碍物的速度。

所以你现在要做的是:总是以相同的速度生成障碍物,然后每个生成的障碍物检查是否轮到他来提高速度。

但是您不希望让一个对象负责该生成和时间控制,然后,当生成器生成障碍物时,它会分配速度。

就像是:

public class ObstacleSpawner : MonoBehaviour
{
     // Keeps track of the time and increases speed based off the time
     float time = 0;
     int seconds = 0;
     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

     //Prefab of the object
     GameObject obstaclePrefab = null;

     //Tracking all the objects
     List<GameObject> obstacleList = new List<GameObject>();

     void InstantiateNewObstacle()
     {
        GameObject newObstacle = Instantiate(obstaclePrefab);
        newObstacle.GetComponent<RigidBody>().velocity = currentSpeed;
        //If you want to Lock the position (that should not be necessary, do it here too)
        obstacleList.Add(newObstacle);
     }

     // Update is called once per frame
     void Update()
     {

         TrackTime();
         if (seconds == timeInterval) {
             currentSpeed = SpeedUp(currentSpeed);
             time = 0;
         }
         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");
     }
}

现在,每次您想要生成一个新的障碍物时,您只需调用ObstacleSpawner.InstantiateNewObject();. 请记住,这obstacleList不是必需的,但我很确定这对您的游戏类型很有用。


推荐阅读