【发布时间】: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