How to speed up enemy after time?
Hello, my question is, how can i change my enemy speed, so it will get faster and faster, atm it is -8 (because it is inverted and enemy moves from up to down). For example, my 1st enemy comes with speed -8, next one already with -9 or smth, and so on. I tried InvokeRepeating, but it doesnt work the way i want to, so i commented it out atm. Thank you for your help! :)
using UnityEngine; using System.Collections;
public class enemyCarMove : MonoBehaviour {
float speed = -8f;
// Use this for initialization
void Start () {
//InvokeRepeating ("speedy", 4.0f, 1.0f);
}
//void speedy () {
// speed = speed + -5f;
//}
// Update is called once per frame
void Update () {
transform.Translate (new Vector3(0, 1, 0) * speed * Time.deltaTime);
}
}
Answer by bubzy · Mar 05, 2016 at 09:21 PM
try
float timeToIncrease = 1.0f; //this is the time between "speedups"
float currentTime; //to keep track
float speed = 0f; //current Speed
float speedIncrement = 5f; //how much to increase the speed by
void Start()
{
currentTime = Time.time + timeToIncrease;
}
void Update()
{
if (Time.time >= currentTime)
{
speed += speedIncrement;
currentTime = Time.time + timeToIncrease;
}
//your movement code here
}
Thank you for your help, but atm enemy speeds up while moving from up to down. What I wanted to make, was that the spawned enemy gets faster speed every time it spawns. Like in the app called "Don't touch the white tile"- tiles are moving faster and faster, as the score gets bigger. For example if my 1st enemy comes down with speed 5, then the next ones should be faster, like 10th comes with speed already with 15 for example. I hope you could help me with this also :).
ok, but for this you would be better off having a spawner script and a enemy script, so that the spawner passes the speed to the enemy as it is spawned like
using UnityEngine;
using System.Collections;
public class enemyScript : $$anonymous$$onoBehaviour {
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position += new Vector3(0, speed*Time.deltaTime, 0); //or however you wish to do this
}
}
attach that to en enemy and
using UnityEngine;
using System.Collections;
public class enemySpawner : $$anonymous$$onoBehaviour {
float speed = 0;
float speedIncrement = 0.5f; // or whatever
public GameObject enemy;
public float timeDelay = 1.0f; //for the purpose of spawning at intervals, you can use what you like :)
public float currentTime;
// Use this for initialization
void Start () {
currentTime = Time.time + timeDelay;
}
// Update is called once per frame
void Update () {
if (Time.time > currentTime)
{
GameObject tempEnemy = Instantiate(enemy, transform.position, Quaternion.identity) as GameObject;
tempEnemy.GetComponent<enemyScript>().speed = speed;
speed += speedIncrement;
currentTime = Time.time + timeDelay;
}
}
}
this to the spawner then assign the enemy gameobject to the spawner in the inspector.
THAN$$anonymous$$ YOU VERY $$anonymous$$UCH!! :) Now it almost works as i want to, thank you!!! :)
Your answer
Follow this Question
Related Questions
Stop movement on collision 1 Answer
how can i repeat an if statment for x sec? 1 Answer
Player doesn't move 0 Answers
Skid steer type movement 1 Answer