Change the speed of movement

I not really good at English, so i apologize for every mistake i done here.

But I do not like to go strictly by the lesson, because I feel that I do not find out a lot of what I would like to.
I’m stuck on an incredibly simple problem.
I just want to increase the speed of meteorites, that go against a player with every wave.
I have a .cs file Mover and .cs file GameController. Inside I have class Mover and class GameController. I need to change the speed of movement, which is in the Mover file and defined as:
public float speed;

And to make the asteroid to move with a new speed.
I have already tried this option in GameController.cs:
Mover o = new Mover ();
o.speed = o.speed + 10;
With this option, the speed does not change.

So how do I do what I have in mind: to change the speed by another class (if done with delays, then every 10 seconds)?

Sometimes small numbers just don’t do it, because it’s hard to notice any change.
Try multiplying by something, or adding big numbers (compared to the original speed).

You can store a static variable for speed inside Mover and access it using class scope :

// declare speed inside Mover
public static float speed = 0f;

// write this where you want to increase it
Mover.speed += 10f;

Thank you very ,much for replies.

Speed “-5” is already fast enough. And if I increase at least for a “-1” each wave it would be fine. As I look in the inspector there are no changes of speed at all.

Ive just tryed, didnt work. It says:
Assets/Scripts/GameController.cs(25,41): error CS0120: An object reference is required to access non-static member `MoveAst.speed’
And on this line i have this code:
MoveAst.speed -= 100f;

I made new file just for asteroids, so now i can put the value in the script.
public float speed = -5f;

How to solve this problems?

Thank you for your attention!

Maybe someone can help in some sort of chat service or skype etc.
I’d be very glad for help like this…
I really want to know more than have tutorials can give me. because after them i wont be able to make something brand-new and awesome. Look, i cant even change speed xD

Static field is a good option, try again. Create public class for your public stuff. Put it in any script you use, on the very top, under “using…” statements.

public static class GlobalVariables{
public static float meteoritesSpeed=-5;
}

in code for your Meteorites:

void FixedUpdate(){
transform.velocity.z=GlobalVariables.meteoritesSpeed;
}

And in your game controller

void Update(){
GlobalVariables.meteoritesSpeed=WhateverValueYouWant;
}

This is not the most christian way to do things. Using static variable in Unity, espacially for things like speed are not the way to go, but it will work while you learn.

1 Like

It is very smart to use global variables, i forgot about them at all. Thank you!

But new problem appears, as always…
All the asteroids began randomly scatter in all directions, instead of going straight.
It solves the problem when I delete FixedUpdate method. You know, it just goes with the same speed.

Maybe you know how to make it work correctly?

I looked at my code, it is wrong) You can’t write:

transform.velocity.z=GlobalVariables.meteoritesSpeed;

It should have given you an error.

it should look something like

void FixedUpdate(){
transform.velocity=new Vector3(transform.velocity.x,   transform.velocity.y,   GlobalVariables.meteoritesSpeed);
}

And I am assuming meters are going in you direction from positive Z direction. In other cases this would send rocks elsewhere.

I am noob in Unity myself, going around forums to learn, don’t be surprised if someone will come and say what I recommending is not the propper way.

But this will work, i made a game where you need to avoid asteroids when doing Space Shooter tutorial. And i used global variables to set speed of all meteors this way.

1 Like

Man, you`re the best! It works! Thank you!

Yeah, there was an error, but I tried to give Unity a link to prefab. Even with that it didn`t work.

Yeah, you`re right =)

I noob twice then =)

Great! Helped a person, now can go sleep.

1 Like