TheLost Lostit TheLost Lostit - 2 months ago 7
C# Question

Why when moving objects forward they are moving very slow ? How can i change the speed?

In

Update
function:
The
UpdateSpheres
create the objects and then move them, but they are moving very slow.

private void Update()
{
UpdateSpheres();
MoveShips();
}

private void MoveShips()
{
var spheres = GameObject.FindGameObjectsWithTag("MySphere");
foreach (Transform child in spheres[0].transform)
{
child.transform.position += Vector3.forward * Time.deltaTime;
}
}

Answer

Calling FindGameObjectsWithTag in every frame is very slow. Call that once in the Start function. Also add a public speed variable you can use to multiple to change the speed. You can modify this speed variable from the Editor until you get the speed you want. Make sure to look in the rest of your script and make sure that you are not using GameObject.Find, FindGameObjectsWithTag or similar functions in the Update function.

GameObject[] spheres;
public float moveSpeed = 50;

void Start()
{
    spheres = GameObject.FindGameObjectsWithTag("MySphere");
}

private void Update()
{
    UpdateSpheres();
    MoveShips();
}

private void MoveShips()
{
    foreach (Transform child in spheres[0].transform)
    {
        child.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
    }
}