【问题标题】:How do I increase the speed an instantiated object spawns in with?如何提高实例化对象的生成速度?
【发布时间】:2020-09-06 07:15:57
【问题描述】:

我正在尝试制作一个无尽的跑步游戏,其中玩家静止在平台上,并且对象以不同的模式在平台下方以初始速度实例化。我希望这些物体在达到一定的时间间隔后提高它们的速度。例如,如果时间间隔为 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


    【解决方案1】:

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

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

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

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

    类似:

    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 不是必需的,但我很确定这对您的游戏类型很有用。

    【讨论】:

    • 感谢您的回复!所以我有生成点的代码,它们实际上是在实例化障碍物,你是说我应该在创建它们的代码而不是它们的移动代码中做所有这些事情?另外,为什么不需要锁定他们的位置?我发现它们在沿着路径移动时会稍微旋转,所以我做了一种蛮力方法来阻止它们四处移动。再次感谢您的帮助!
    • 我想说的是,多个对象的实例化应该由另一个控制游戏状态(currentSpeed)的实体来完成。运动可以由每个单独的障碍物完成(如果需要)。我不知道你的障碍是如何回答最后一个问题的(也许应该是一个新问题),但如果它们是静态的,则默认情况下它们不应该移动(也许它们有一个刚体并且它没有标记为 Kinematic ?)。最后,请记住将此答案标记为正确(如果正确)以帮助其他人。
    • 谢谢。我相信代码只会实例化对象并在实例化时设置速度。之后,障碍物减速并停止。我如何使它成为一个恒定的速度/力?
    • 使用刚体组件如 rb.velocity = new Vector3(0, 10, 0);
    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 2019-11-22
    • 2022-10-19
    • 2016-11-23
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多