Home > front end >  How to make a list of gameObjects disappear animated? (or how to fix weird behaviour when setting al
How to make a list of gameObjects disappear animated? (or how to fix weird behaviour when setting al

Time:12-09

I am trying to make a list of gameObjects disappear when the player enters a room, the best way I could think about doing it, was changing the material alpha frame by frame. If there is an alternative to this method that is more performatic, please tell! (not that I noticed any bad performance as this seems simple enough)

I expect the player to enter a room and all of the gameObjects in that list to disappear at the same time and at the same rate (as if they were all just one object having their transparency changed in runtime) but what happens is (I've made a video: https://youtu.be/z1pz2Te_hDg ) that some gameObjects change before others, some disappear way faster than expected and just in the end after the others, it all looks weird.

Changing the alpha of only one object at a time seems fine, but since I need to loop through all of the materials, I can't simply put all objects as a child of one gameObject.

I've tried to change the alpha without making a custom shader with an alpha property by accessing the default alpha in the default "HDPR/Lit" shader and it behaves the same.

This problem seems easy, I feel like I am doing something stupidly wrong but I've been at this for a couple of weeks.

Here is my "RoomTransparencyController" script:

bool transparent = false;
public float enhance = 5;
private struct ShaderPropertyIDs {
    public int AlphaRef;
}
private ShaderPropertyIDs shaderProps;
public List<GameObject> gameObjectList;
public List<Material> materialList;

void Start() {
    foreach(GameObject obj in gameObjectList) {
        foreach(Material mat in obj.GetComponent<MeshRenderer>().materials) {
            materialList.Add(mat);
        }
    }

    // Cache property IDs
    shaderProps = new ShaderPropertyIDs() {
        AlphaRef = Shader.PropertyToID("AlphaRef"),
    };
}

void Update() {
    UpdateChildsAlpha3(transparent);
}
private void OnTriggerEnter(Collider col) {
    if(col.tag == "Player") {
        transparent = true;
    }
}

private void OnTriggerExit(Collider col) {
    if(col.tag == "Player") {
        transparent = false;
    }

}

void UpdateChildsAlpha3(bool transparent) {
    foreach(Material mat in materialList) {
        mat.SetFloat("AlphaRef", Mathf.Clamp(mat.GetFloat("AlphaRef")   Time.deltaTime * enhance * (transparent ? -1 : 1), 0, 1));

    }
}

PS: This script should mainly contain props and specific walls from a room so there is a script in every room with its gameObject list for better organization. So I thought that each room having its script was the best way to organize and replicate.

CodePudding user response:

If you want the objects to be gone, then use a for loop with Destroy() on all the objects, and if you want them to stop being visible, just use SetActive(). If you want them to work, but just stop being visible, you can disable the renderer component.

CodePudding user response:

It will be much better if you try to use some tweens to solve this problem. Like doTween or LeanTween, where they have some better-optimized way to reduce alpha or anything. Try to play around with it.

  • Related