Problems with movement causing super lag

Hello all,
I attached the new script to my asteroids and now everything is lagging on the level. Any idea why Unity can’t handle all the movement?
It works fine on single asteroids. But when applied to the prefab it breaks.
Thank you,
Adam

Hi Weiprecht,

How many tris/vertices do your asteroids have? If their mesh is too complex, Unity might have a lot to do to render them.

And what happens if you disable the script on the asteroids?

Hello Nina,

I don’t know what is considered a high tris/vertices level. I have attached the stats below. I have only one mesh on the rigidbody. Thoughts? I am very new to this and optimization should be something I learn about.
Thank you for your assistance,
Adam

image

704,700 tris sounds a lot for a little game like this. What is a try? Imagine you had a simple quad consisting of four vertices. That’s 2 or more triangles (tris). 5 vertices can form at least 3 triangles, etc. The number of tris depends on how the vertices are connected with one another.

The number of vertices/tris gives you an idea about the complexity of a mesh. The more complex a mesh is, the more Unity has to calculate.

However, according to the stats, you have an framerate of approx. 661, which is fairly high. The game should not be laggy. It might be slightly laggy because the game window is just a preview. To check if the actual game is laggy, build your game and test the standalone version.

Try to disable the Astroid Movement to see whether the issue is caused by the script. Without knowing anything about the content of the script, it is impossible to say whether there is an issue with it.

Thank you Nina,

Here is the script. It is the same as what we discuss in class except for a angularVelocity method line. Is it possible that the lag happened because the asteroids were bumping into each other? I have about 30 asteroids on the second level.
Thank you,
Adam

public class AstroidMovement : MonoBehaviour
{
    [SerializeField]
    private float tumble;
    [SerializeField] Vector3 movementVector = new Vector3(10f, 10f, 10f);
    [SerializeField] float period = 20f; // this creates a Serialized var called period and sets its standard for 2 sec.

    //ToDo remove from inspector later
    [Range(0, 1)]
    [SerializeField] //these modify the variable below
    float movementFactor; // 0 for not moved, 1 for fully moved.
    Vector3 startingPos; //stores starting position of the object so you can set movement
    // Start is called before the first frame update
    void Start()
    {
        startingPos = transform.position; //sets starting position
        GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble;
    }

    // Update is called once per frame
    void Update()
    {
        if (period <= Mathf.Epsilon) { return; }
        float cycles = Time.time / period;  //create a variable called cycles that = the game time (Time.time) / var period
        const float tau = Mathf.PI * 2; // about 6.28
        float rawSinWave = Mathf.Sin(cycles * tau); // protect against period = zero
        movementFactor = rawSinWave / 2f + 0.5f;
        Vector3 offset = movementVector * movementFactor; //indicates the level of offset
        transform.position = startingPos + offset; // add the offset to the starting position

    }
}

That might well be the case. I assume they have colliders attached, haven’t they? If so, the physics engine tries to calculate collisions among the asteroids. What you could try is to give the asteroid an “Astroid” tag and to modify the layer collision matrix so the asteroids don’t collide with one another.

Thank you Nina,

I just implemented the layer collision matrix and it seems to have taken care of the lag. Everything is moving correctly now.
Thank you for the guidance,
Adam

I’m glad I could help. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms