我的目标是进行一次碰撞检测,以降低其与特定持续时间相撞的对象的运动速度。
我到目前为止尝试的是:
//Class that detects the collision
if (other.gameObject.tag == "enemy")
{
EnemyMovement enemyMove = other.GetComponent <EnemyMovement> ();
if (enemyMove.slowMove != 1) {
return;
}
enemyMove.Slow (2, 0.5f, true);
//....
//Class that handles the Enemy-Movement
//The slowfactor gets multiplied with the Vector3 that handles the movementspeed of the Character
void FixedUpdate ()
{
Movement();
}
void Movement()
{
gegnerRigid.MovePosition (transform.position + (gegnerMove * slowMove));
}
public void Slow (float duration, float slowFactor, bool slowed)
{
if (slowed)
{
if (duration > 0) {
duration -= Time.deltaTime;
slowMove = slowFactor;
Slow (duration, slowFactor, slowed); //this recursive Call leads to huge performance issues, but how to keep the function going?
} else {
slowMove = 1;
slowed = false;
}
}
}
所以我想发生的事情:如果发生冲突,请调用慢速功能,并使其自身调用,直到持续时间为0。
注意,这里的关键是
1。您在另一个对象上有buff/unnuff 。
您只需从" boss"对象调用另一个对象。请勿将实际的buff/nunf code放入您的"老板"对象中。正义的"呼吁"。
换句话说:在您要抛光/不封闭的事物本身上始终具有buff/unnuff code。
2。对于Unity的计时器,只需使用"调用"或" InvokerePeating"。
真的很简单。
buff/unnfuld很简单:
OnCollision()
{
other.GetComponent<SlowDown>().SlowForFiveSeconds();
}
在您要放慢的对象上...
SlowDown()
{
void SlowForFiveSeconds()
{
speed = slow speed;
Invoke("NormalSpeed", 5f);
}
void NormalSpeed()
{
speed = normal speed;
}
}
如果您想"慢慢慢"它- 不。不可能注意到在视频游戏中。
从理论上看
SlowDown()
{
void SlowlySlowForFiveSeconds()
{
InvokeRepeating("SlowSteps", 0f, .5f);
Invoke("NormalSpeed", 5f);
}
void SlowSteps()
{
speed = speed * .9f;
}
void NormalSpeed()
{
CancelInvoke("SlowSteps");
speed = normal speed;
}
}
这很简单。