我试图使敌人的移动速度快于某些得分点之前的速度。我是一个新手程序员,所以如果代码看起来丑陋,请忍受。如果有人可以帮助我解决这个问题,那将真的很棒。谢谢。
这是我的得分管理代码:
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.
}
}
答案 0 :(得分:0)
谢谢你们,我知道了。我是这样做的。
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.
}