加快每个预制件的速度

时间:2018-11-14 19:36:09

标签: c# visual-studio unity3d

大家好,我统一从事一些反射攻丝游戏,我需要为每2个或更多的预制销毁脚本增加播放器的速度,并且我需要恒定地为2个prefbas增加速度。有人可以帮我吗,我这样做:

void OnTriggerEnter(Collider other)
{

        if (other.gameObject.CompareTag("Sphere"))
        {

        count = count + 1;

        if(count >= highScore)
        {
            highScore = count;
            PlayerPrefs.SetInt("highscore", highScore);
            PlayerPrefs.Save();
        }

        SetCount();


        if(count == 2)
        {
            rb.AddForce(0, 0, 50 * forwardForce * Time.deltaTime);
        } 

}

1 个答案:

答案 0 :(得分:2)

我建议将Speed设为您的Player类的属性:

public class Player : MonoBehaviour
{
    public float Speed;
}

在移动播放器时,您需要确保乘以该属性(我假设您的代码与此类似):

rigidbodyReference.AddForce(0, 0, 50 * Speed * Time.deltaTime);

然后,我将创建一个静态GameManager类,该类将跟踪预制件的数量:

public static class GameManager : MonoBehaviour
{
    public static PrefabCount;
}

最后,我将添加一个speedIncrement变量(暴露给检查器)作为您的Player类的属性,并修改您的OnTriggerEnter方法:

public float speedIncrement;

// ...

void OnTriggerEnter(Collider other)
{
    // This will reduce unnecessary nesting in your code to make it easier to read
    if (!other.gameObject.CompareTag("Sphere"))
        return;

    // Same thing as GameManager.PrefabCount = GameManager.PrefabCount + 1
    GameManager.PrefabCount++; 

    if (GameManager.PrefabCount >= highScore)
    {
        highScore = GameManager.PrefabCount;
        PlayerPrefs.SetInt("highscore", highScore);
        PlayerPrefs.Save();
    }

    // Use the Modulus operator to determine if the PrefabCount is evenly divisible by 2
    if (GameManager.PrefabCount % 2 == 0)
        Speed += speedIncrement; // Increase speed by whatever value set in the inspector
}