Question by
gauravpela · Nov 28, 2018 at 06:09 PM ·
c#unity 5visual studio
How to increase the movement speed of enemy based on score?
I am a novice programmer, so if anyone could help me get through with this it would be really awesome. Thank You.
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"))
{
highScore = PlayerPrefs.GetFloat("HighScore");
}
}
void Update()
{
if (scoreIncreasing)
{
score += pointsPerSecond * Time.deltaTime;
}
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetFloat("HighScore", highScore);
}
scoreText.text = ((int)score).ToString() + " Clicks";
}
}
and here is my enemy mover code:
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;
FindObjectOfType<ScoreManager>();
baseSpeed *= multiplier;
}
void Update ()
{
GoUp();
EnemyMovement();
}
public void EnemyMovement()
{
if(FindObjectOfType<ScoreManager>().score > scoreToNextLevel)
{
NewSpeed();
SpeedUp();
}
multiplier++;
}
public void GoUp()
{
transform.position += Vector3.up * baseSpeed * Time.deltaTime;
}
public void NewSpeed()
{
newSpeed = baseSpeed * multiplier * Time.deltaTime;
}
public void SpeedUp()
{
transform.position += Vector3.up * newSpeed;
}
}
Comment
Your answer
Follow this Question
Related Questions
VS Code or visual studio? 1 Answer
How to see class & member information on hover in Visual Studio? 0 Answers
How to change C# version in Visual Studio with Unity Project 0 Answers
transform.localscale to specific size 1 Answer
How to Disconnect a client from the server properly using unity Netcode for GameObjects ? 0 Answers