如何让恒力随时间增加? (统一 5)

How to Get Constant Force To Increase Over Time? (Unity 5)

我的游戏是一款超跑无尽的跑酷游戏。我用恒定的力使障碍物从屏幕上下来。我的问题是,如何让这个 Constant Force Script 使障碍随着游戏的进行和玩家进入游戏的时间变长而逐渐变快?如果您自己尝试这个脚本(只需将它附加到一个立方体或一个球体上),您会注意到它一开始是如何缓慢开始的,然后实际上 2 秒后,它进入了 Rocket Speed 哈哈。 (我也在研究对象池脚本,因为我不想在实例化和销毁上浪费 CPU)感谢所有尝试帮助我解决这个问题的人! :)

这是我制作的脚本:

using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(ConstantForce))]

 public class AddConstantForce : MonoBehaviour
 {
     float speed = 1.0f;
     Vector3 force;

     void Awake()
     {
         //Another way to "Require" the component.
         if(!GetComponent<ConstantForce>())
             gameObject.AddComponent<ConstantForce>();
         //
         force = GetComponent<ConstantForce>().force;
         GetComponent<Rigidbody>().useGravity = false;
     }

     void Update()
     {
         GetComponent<ConstantForce>().force = force;

         force += -transform.forward * speed * Time.deltaTime;
         /* Transform is negative because in my game,  I have the
            illusion of the objects coming down my mountain.
         */   
     }
 }

最初我打算建议使用 Time.time 作为因素(因为 Time.time 不断增加),但是,如果您必须等待 运行 开始的一些输入, Time.time 在 运行 实际开始的时间可能已经很大了。

相反,我建议保留第三个变量作为 Time.time(每帧更新 Time.deltaTime)并使用第三个变量不断增加施加的力。

例如:

 float speed = 1.0f;
 Vector3 force;
 float time = 0;

 void Awake()
 {
     //Another way to "Require" the component.
     if(!GetComponent<ConstantForce>())
         gameObject.AddComponent<ConstantForce>();
     //
     force = GetComponent<ConstantForce>().force;
     GetComponent<Rigidbody>().useGravity = false;
 }

 void Update()
 {
     GetComponent<ConstantForce>().force = force;

     time += Time.deltaTime;
     force += (-transform.forward * speed * Time.deltaTime) * time;
     /* Transform is negative because in my game,  I have the
        illusion of the objects coming down my mountain.
     */   
 }

注意:您可能需要为 speed 应用更大的值,因为此操作将 return 是一个非常小的值。或者您可以添加另一个变量,该变量将增加操作所产生的小值 return。还要考虑一旦开始测试难度,您可能需要使用比 Time.deltaTime 更大或更小的值来增加 time,以使游戏更难或更容易。

通常,要做这样的事情,你需要 2 个变量:

1。您希望速度增加的时间。(例如,每 5 秒增加一次)secondsToIncreaseIn.

2。要增加的值。(例如,在#1 秒内每次将速度增加 2)valueToIncreaseBy.

这样您就可以灵活地通过更改任一变量来加快或减慢速度增量。通过实验设置 secondsToIncreaseIn5valueToIncreaseBy0.0007f 使游戏持续时间更长。您可以增加 valueToIncreaseBy 使其均匀持续 longer.You 如果启动速度太慢或太高,可以更改 startingSpeed 变量。

private float speed = 0f;
private Vector3 force;
private ConstantForce cachedConstantForce;
private float counter = 0; //Dont change(Used for counting)

public float startingSpeed = 5f;//Start with 5 speed
public int secondsToIncreaseIn = 5; //Increase in every 5 seconds
public float valueToIncreaseBy = 0.0007f; //Increase by 0.0007 in secondsToIncreaseIn time/seconds



void Awake()
{
    //Another way to "Require" the component.
    cachedConstantForce = gameObject.GetComponent<ConstantForce>();

    if (cachedConstantForce == null)
    {
        gameObject.AddComponent<ConstantForce>();
    }


    cachedConstantForce = GetComponent<ConstantForce>();
}

void Start()
{
    force = cachedConstantForce.force;
    GetComponent<Rigidbody>().useGravity = false;
    speed = startingSpeed;
}

void Update()
{
    cachedConstantForce.force = force;

    force = -transform.forward * speed * Time.deltaTime;
    /* Transform is negative because in my game,  I have the
       illusion of the objects coming down my mountain.
    */

    if (counter < secondsToIncreaseIn)
    {
        counter += Time.deltaTime;
    }
    else
    {
        //Time has reached to Increase value
        counter = 0; //Reset Timer
        //Increase Spped
        speed += valueToIncreaseBy;

        Debug.Log("Increased Speed!");
    }
}