Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据分数提高敌人的移动速度?_C#_Unity3d - Fatal编程技术网

C# 如何根据分数提高敌人的移动速度?

C# 如何根据分数提高敌人的移动速度?,c#,unity3d,C#,Unity3d,我试图让敌人的移动速度比得分前更快。我是一个新手程序员,所以如果代码看起来很难看,请忍受它。如果有人能帮我度过难关,那将是非常棒的。多谢各位 以下是我的分数管理代码: public class ScoreManager : MonoBehaviour { public float score; public float pointsPerSecond; public float highScore; public bool scoreIncreasin

我试图让敌人的移动速度比得分前更快。我是一个新手程序员,所以如果代码看起来很难看,请忍受它。如果有人能帮我度过难关,那将是非常棒的。多谢各位

以下是我的分数管理代码:

public class ScoreManager : MonoBehaviour 
{
     public float score;
     public float pointsPerSecond;
     public float highScore;

     public bool scoreIncreasing;

     public Text scoreText;

     void Start()
     {
         score = 0;

         if (PlayerPrefs.HasKey("HighScore")) // checks if player has HighScore or not
         {
             highScore = PlayerPrefs.GetFloat("HighScore"); //gets the value of stored highscore.
         }
     }

     void Update()
     {        
         if (scoreIncreasing) //checks if score is increasing or not
         {
             score += pointsPerSecond * Time.deltaTime; //increases score as per time
         }

         if (score > highScore) // checks if score is greater than highscore or not
         {
             highScore = score; //sets value of score to highscore
             PlayerPrefs.SetFloat("HighScore", highScore); // stores the value of highscore
         }
         scoreText.text = ((int)score).ToString() + " Clicks"; 
         //provieds the value of score to score text object.
     }    
}
这是我的敌机代码:

public class EnemyMover : MonoBehaviour
{    
     [SerializeField]
     private float tumble;

     public float baseSpeed = 2f;
     public float newSpeed;
     public float multiplier;

     public float scoreToNextLevel = 10f;

     void Start ()
     {
         GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble; //makes the object rotate
         FindObjectOfType<ScoreManager>(); //caches the scoremanaging script
         baseSpeed *= multiplier; //gives basespeed a value eg baseSpeed(2) * multiplier(2) = 4.
     }

     void Update ()
     {
         GoUp(); //calls method goup
         EnemyMovement(); //calls method enemy movement
     }

     public void EnemyMovement()
     {
         if(FindObjectOfType<ScoreManager>().score > scoreToNextLevel)    
         //checks whether the condition is true
         {
             NewSpeed();  //calls method newspeed
             SpeedUp();    // calls method speedup
         }
         multiplier += 1;  // increases the value of multiplier by 1
     }

     public void GoUp()
     {
         transform.position += Vector3.up * baseSpeed * Time.deltaTime;  //moves the enemy object upwards in y axis.
     }

     public void NewSpeed()
     {
         newSpeed = baseSpeed * multiplier * Time.deltaTime; // proves new speed with a value.     
     }

     public void SpeedUp()
     {
         transform.position += Vector3.up * newSpeed; // moves enemy object upwards into y axis using new speed.   
     }
}
公共类EnemyMover:MonoBehavior
{    
[序列化字段]
私人浮子翻滚;
公共浮动基准速度=2f;
公共浮动新闻速度;
公共浮动乘数;
公共浮点数为10f;
无效开始()
{
GetComponent().angularVelocity=Random.insideUnitSphere*tumble;//使对象旋转
FindObjectOfType();//缓存记分管理脚本
baseSpeed*=乘数;//给baseSpeed一个值,例如baseSpeed(2)*乘数(2)=4。
}
无效更新()
{
GoUp();//调用方法GoUp
EnemyMovement();//调用敌方移动方法
}
公共卫生运动
{
if(FindObjectOfType().score>scoreToNextLevel)
//检查条件是否为真
{
NewSpeed();//调用方法NewSpeed
SpeedUp();//调用方法SpeedUp
}
乘数+=1;//将乘数的值增加1
}
公屋
{
transform.position+=Vector3.up*baseSpeed*Time.deltaTime;//沿y轴向上移动敌方对象。
}
公共无效新闻速度()
{
newSpeed=baseSpeed*multiplier*Time.deltaTime;//用一个值证明新速度。
}
公共空间加速()
{
transform.position+=Vector3.up*newSpeed;//使用新速度将敌方对象向上移动到y轴。
}
}

谢谢你们,我想出来了。我是这样做的

void Update ()
{
    if (FindObjectOfType<ScoreManager>().score > scoreToNextLevel) // checks if score 
is greater than score to next level, if yes then following codes are applied.
    {            
        multiplier += 1f; // increases the value of multiplier by 1.
        baseSpeed += multiplier;// adds the multiplier value to basespeed.
        EnemyMovement();//calls the enemymovement method
        scoreToNextLevel *= 2;// multiplies the score to next level by 2.
    }
void更新()
{
if(FindObjectOfType().score>scoreToNextLevel)//检查是否得分
大于下一级别的分数,如果是,则应用以下代码。
{            
乘数+=1f;//将乘数的值增加1。
baseSpeed+=乘数;//将乘数值添加到baseSpeed。
EnemyMovement();//调用EnemyMovement方法
scoreToNextLevel*=2;//将下一级别的分数乘以2。
}

得分
值传递给敌人,然后在你的一个速度方法中进行计算。我这样做了,但我的乘数被无限增加,这会导致我的敌人移动得比我看到的快。无关提示:注意标记的描述:如果你对Visual Studio功能和f有特定问题,请使用此标记功能性。不要在有关仅在Visual Studio中编写的代码的问题上使用此标记。因此,它不适用于此问题。只是一个通用的统一提示,不要在代码中使用FindObjectOfType(每帧都会执行),所有查找方法的性能都很差。请将ScoreManager设置为您的序列化字段r类。我还想问你是否需要在更新中检查两个数字之间的差异-你不能在分数增加后创建一个事件来进行此检查吗?谢谢你,@Steven Coull的提示,非常感谢。我对这个统一和编程非常陌生,我创建什么事件来检查t的差异wo编号?您的类的任何通用方法。
public void ChangeSpeed(float newSpeed){currentSpeed=newSpeed;}