0
\$\begingroup\$

Is there a way of limiting my speeding my enemy script every time my player collects 10 points. I have a movement script attached to my enemies and every time my player collects 10 points my enemies get faster. I want to put a limit on that so that the movement script (attached to my enemies) doesn't increase all the time even if the player continue to collect 10 more points and onwards. I want the limit to be 40, so even if the player continues to collect another 10 points the movement should stay at 40.

This is my speeding up my enemies script (this is used in my score script):

if (Score % 10 == 0){
    //Increment movespeed variable  from Movement script
    Movement.movespeed += 4;
} 

And this is my movement script attached to my enemies:

public static int movespeed = 20;
public Vector3 userDirection = Vector3.right;

//public float lifetime;

void Start ()
{
    //Destroy (gameObject, lifetime);
}

public void Update()
{
    transform.Translate (userDirection * movespeed * Time.deltaTime); 
}

How can I limit the movement speed so that it doesn't exceed the upper limit I want?

\$\endgroup\$
0
\$\begingroup\$

Yes, this is pretty easy. You want to clamp the movement speed to never exceed your limit 40, so all you have to do is to tweak your increment code a little. Instead of movespeed += 4;, you want to change that line to movespeed = Mathf.Min(movespeed + 4, 40); That code picks the smallest value out of [40, movespeed + 4], which will be the speed to use. If movespeed + 4 is more than 40, 40 is the smaller value and will be used instead.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.