Increasing Enemy Speed
I have enemy script that includes the speed. I can get the original script to work, but I wanted to add some difficulty to my game. I tried to increase the speed based on the user's score, but now the enemy does not move at all. I have tried several different things and cannot get the enemy to move. The enemy spawns, but just stands still. What am I doing wrong or what could I add to fix this issue?
Here's my code:
Working Code:
using UnityEngine;
using System.Collections;
public class RightGloves : MonoBehaviour {
public GameObject glove;
//public Vector3 target;
public static float increaseSpeed=0;
private float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
speed = Random.Range (6, 10);
float change = (speed+=increaseSpeed ) * Time.deltaTime;
glove.transform.Translate(Vector3.left *change );
Destroy (gameObject,4);
}
}
Non - Working Code:
using UnityEngine;
using System.Collections;
public class LeftGloves : MonoBehaviour {
public GameObject glove;
//public Vector3 target;
public static float increaseSpeed=0;
private float speed;
private int level=0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (ScoreManager.scoreCount >= 10000 && ScoreManager.scoreCount < 20000 && level ==0) {
level++;
speed = (Random.Range (6, 10)) * Time.deltaTime;
//float change = (speed+=increaseSpeed ) * Time.deltaTime;
glove.transform.Translate(Vector3.right * speed);
Destroy (gameObject,4);
}
if (ScoreManager.scoreCount >= 20000 && ScoreManager.scoreCount < 50000 && level ==1) {
level++;
speed = (Random.Range (8, 12)) * Time.deltaTime;
//float change = (speed+=increaseSpeed ) * Time.deltaTime;
glove.transform.Translate(Vector3.right *speed);
Destroy (gameObject,4);
}
if (ScoreManager.scoreCount > 50000 && level ==2) {
level++;
speed = (Random.Range (10, 14)) * Time.deltaTime;
//float change = (speed+=increaseSpeed ) * Time.deltaTime;
glove.transform.Translate(Vector3.right *speed);
Destroy (gameObject,4);
}
}
}
Whisper _Leaf's answer worked and I also removed the private level variable. I used private level in another script where I was instantiating objects. I realized I did not need it this case because the speed will remain the same so long as the if statements is true. When instantiating, a true if statement (in my case) caused the objects to continue instantiating until the if statement was false. Sometimes simple hiccups cause catastrophic results :)
THAN$$anonymous$$ YOU ALL FOR YOUR HELP!
Answer by Whisper_Leaf · Jun 27, 2016 at 01:46 PM
You're calling level++; whenever your If statement returns true, which means the very next frame, it will be false. And if none of the Ifs return true, your object doesn't move. You can fix this by moving glove.transform.Translate(Vector3.right *speed); Destroy (gameObject,4); out of the If block. Make sure you also move the Time.deltaTime into the Translate, otherwise you'll be using the old deltaTime.
Answer by doghell · Jun 27, 2016 at 01:46 PM
It's difficult to pinpoint exactly what the issue is since I have no reference to the default scoreCount value, but I am pretty confident the issue here is none of the if statement conditions are being met.
if ScoreManager.scoreCount is less than 10000 nothing will happen.
Since each of your conditions seem to run individually, you might as well put them in an if ... else with a default in case none of the above are reached.
A good place to start debugging this issue is also to add a breakpoints or even Debug.Log statements.
Answer by Dibbie · Jun 27, 2016 at 06:08 AM
Technically, speed starts at 0 because you never set it, and by the looks of it, you destroy the object after the player (or this object) "levels up", so im assuming you respawn the enemy in a different script... In which these updates are not applied to (so it spawns with the default auto-assigned value of 0)
I would set a a base speed, something like 1f or 5f, initially however fast you want the enemey to move at "level 0", then on every level up, use += instead of =, since each one of your if-statements will only ever run once anyway, += will be far more accurate.
You also never use increaseSpeed and im not sure why its static in my opinion, though you could update one of your if-statements to look something like this:
if (ScoreManager.scoreCount >= 10000 && ScoreManager.scoreCount < 20000 && level ==0) {
speed += ((float)Random.Range (6, 10)) * Time.deltaTime; //Remember that speed is a float, and your ranges are integters, deltaTime is also a float, so for the most accurate calculations, you want to make sure everything is being processed as "floats" technically - hense (float) at the start, which converts the int result of Random.Range to a float.
glove.transform.Translate(Vector3.right * speed);
//Destroy (gameObject,4); //Still not 100% sure why you need to destroy the object
level++;
}
Hope that helps.
Answer by Crimx · Jun 27, 2016 at 09:51 PM
thats because you put
level++;
at update so everytime Update method called (which is at every frame) the level increased while your score don't. I think you should leave the level and use just score rank to determine your level like this
if (ScoreManager.scoreCount >= 10000 && ScoreManager.scoreCount < 20000) level = 0;
else if (ScoreManager.scoreCount >= 20000 && ScoreManager.scoreCount < 50000) level = 1;
else if (ScoreManager.scoreCount > 50000) level = 3;
Then you can if-else statement with this
...
if(level ==1) {
level++;
speed = (Random.Range (6, 10)) * Time.deltaTime;
//float change = (speed+=increaseSpeed ) * Time.deltaTime;
glove.transform.Translate(Vector3.right * speed);
Destroy (gameObject,4);
}
...
or you can just put the statement inside the previous if-else statement. Hope this help and sorry if my english is not good
Your answer
![](https://koobas.hobune.stream/wayback/20220612083735im_/https://answers.unity.com/themes/thub/images/avi.jpg)