• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by El-Deiablo · Jun 27, 2016 at 04:34 AM · enemyspeedscoreincrease

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);
 
         }
 
     }
         
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image El-Deiablo · Jun 27, 2016 at 08:09 PM 0
Share

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!

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Increasing score based on speed 1 Answer

how make the coin not increased when the player touch them more than one? 1 Answer

Score counter not working properly. 1 Answer

Help with making a endles runner 0 Answers

How to immediately CHANGE SPEED from spotted position to another?) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges