Moving game objects

Hi guys, first big sorry for my bad english, im working on it :wink:

I want to ask you something. I want to move some objects in my game, its simple, there are asteroids which move from up to down (create up and destroy down). But they speed is static, for example 5, so if for example I catch box for speed I want to rise this speed from 5 to example 20 and after some time its will be again 5.

My code for speed is:

static var asteroidspeed : float = -5;

function Update () {
rigidbody.velocity = transform.forward * asteroidspeed;
}

and there is code for their creation:

var spawnPosition : Vector3 = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
var spawnRotation : Quaternion = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);

but the problem is, when I change speed from Start() to Update() because if there is Start() asteroids which are already exist dont react on changing speed but asteroids which will be create react on it so I changed it on Update(), but asteroids are acting weird. They rotate around their axis and they dont move from up to down directly but they create something like circle or I dont know :smiley: Simple it looks very strange

I’m new at this too, but it looks like part of the problem is in your Update function. I think Update is called at every animation frame, so the effect of this function is being applied at unpredictable times. Try changing this:

function Update () { rigidbody.velocity = transform.forward * asteroidspeed; }

to this:

function Update () { rigidbody.velocity = transform.forward * asteroidspeed * Time.deltaTime; }

Doing that will multiply the effect of your speed by some number > 0 and < 1 (eg. .005) to reflect that it’s happening once per frame.

I’m less sure about this, but think you should not be using “static” and that you should set the value of asteroidspeed in the Start() function, not in the declaration above it.

If your asteroids have a rigidbody component, don’t use the transform component to move them. Use forces to move them instead (like the AddForce function).
If you still want to move them using the transform component, make sure they are marked as isKinematic (otherwise they will not move).

If you want to move them, try using the RigidBody.MovePosition function. I believe that’s what you are looking for.