Как увеличить скорость передвижения противника по счету? - PullRequest
0 голосов
/ 28 ноября 2018

Я пытаюсь заставить скорость движения противника идти быстрее, чем это было до определенных набранных очков.Я начинающий программист, поэтому, если код кажется вам уродливым, смиритесь с этим.Если бы кто-нибудь мог помочь мне справиться с этим, это было бы действительно здорово.Спасибо.

Вот мой код управления счетом:

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.   
     }
}

1 Ответ

0 голосов
/ 29 ноября 2018

Спасибо, ребята, я понял это.Я сделал это так.

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.
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...