【问题标题】:How to Get Constant Force To Increase Over Time? (Unity 5)如何获得恒定的力量随着时间的推移而增加? (统一 5)
【发布时间】:2016-04-15 12:41:16
【问题描述】:

我的游戏是一个过肩无尽的跑步风格游戏。我用恒定的力量让障碍物从屏幕上下来。我的问题是,如何使这个恒力脚本随着游戏的进行和玩家进入游戏的时间越来越长,使障碍物逐渐变快?如果您自己尝试此脚本(只需将其附加到立方体或球体上),您会注意到它一开始很慢,然后 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.
         */   
     }
 }

【问题讨论】:

  • 好吧,force 应该只设置一次(例如在Start()),如果您尝试随着时间的推移持续加速(逐渐增加速度)。如果这不是您想要的效果,请相应地澄清您的问题。

标签: c# unity3d position unity5


【解决方案1】:

通常,要执行此类操作,您需要 2 个变量:

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

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

通过这种方式,您可以灵活地通过更改任一变量来加快或减慢您的速度增量。实验将secondsToIncreaseIn 设置为 5 并将valueToIncreaseBy 设置为 0.0007f 使游戏持续时间更长。您可以增加valueToIncreaseBy 使其持续更长时间。如果启动速度太慢或太高,您可以更改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!");
    }
}

【讨论】:

    【解决方案2】:

    最初我打算建议使用 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 应用更大的值,因为此操作将返回相当小的值。或者您可以添加另一个变量来增加操作返回的小值。另请考虑,一旦您开始测试难度,您可能需要使用比Time.deltaTime 更大或更小的值来增加time,以使游戏更难或更容易。

    【讨论】:

      猜你喜欢
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 2013-08-30
      • 2019-02-24
      相关资源
      最近更新 更多