ShowTime13
ShowTime13
CC#
Created by ShowTime13 on 3/6/2025 in #help
Is the check if variable is certain value creating performance hit?
So, here is my code in Unity (I run it in Update):
if (currentPlatform == null && ballForce.velocity.magnitude >= 20) {
addForceSpeedAir = addForceSpeedAir * 0.5f;
print("forceStop1");
if (ballForce.velocity.magnitude >= 25) {
addForceSpeedAir = addForceSpeedAir * 0.2f;
print("forceStop2");
}
} else {
addForceSpeedAir = originalAddForceSpeedAir;
}
if (currentPlatform == null && ballForce.velocity.magnitude >= 20) {
addForceSpeedAir = addForceSpeedAir * 0.5f;
print("forceStop1");
if (ballForce.velocity.magnitude >= 25) {
addForceSpeedAir = addForceSpeedAir * 0.2f;
print("forceStop2");
}
} else {
addForceSpeedAir = originalAddForceSpeedAir;
}
This code is like a speed limiter for a ball in a game. Basically, it's doing this: If the ball is flying in the air and going really fast, it turns down the 'air boost' to slow it down a bit. The faster it's going in the air, the more it reduces this boost. But if the ball is on the ground or moving slowly, the 'air boost' goes back to normal. So, my question is: 1. Is it a good way to change a value and then bounce back to the original value? 2. (MAIN) I'm running this in Update function, meaning 60 times per second. I'm worried that I'm going to set addForceSpeedAir to originalAddForceSpeedAir each time the if statement goes to else. So, it's going to set it once, and then each second 60 times. But, in my view only way to escape this is to Check with else if addForceSpeedAir == originalAddForceSpeedAir, but then again, I'll be checking 60 times a second if a value is something, which also sounds pointless. Is there any way to escape this? Basically, I want to set it just once and be done with it. I don't wanna check it 60 times per second, or set it 60 times per second. Events?
78 replies
CC#
Created by ShowTime13 on 2/25/2025 in #help
✅ Logically, is it possible to update the List only if there's a change in the list itself?
What I'm trying to do: I'm trying to get List of items found using a tag (GameObject.FindGameObjectsWithTag("Cube")), and to optimize, I want the list update to only happen if the quantity of gameobjects changed. But the problem I see is that, how would the cubesList check, if only check is by list update, and it is bound to not update if it's same as before. So, here is the code: public class Attraction_test : MonoBehaviour { int previousCount; List<GameObject> cubesList = new List<GameObject>(); // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (cubesList.Count != previousCount) { cubesList.AddRange(GameObject.FindGameObjectsWithTag("Cube")); previousCount = cubesList.Count; } print("cubesList Count "+cubesList.Count); foreach (GameObject cubeList in cubesList) { print("CubesList: " + cubeList.name); } } }
3 replies
CC#
Created by ShowTime13 on 2/25/2025 in #help
✅ Is there a way to use the whole array or list, instead of looping through it?
My goal is to find a lot of objects, then apply certain effect on them all instantaneously. 1. Let's say I'm creating GameObject[] cubes = GameObject.FindGameObjectsWithTag("Cube"); array. Now I have the array in memory which contains cubes[0] to cubes[500]. (or list, if that makes it any different). 2. As I'm asking ChatGPT, it tells me that you can't use all the variables from the array at once and I have to loop through them with for or foreach. How can I create variables for each of the cube internally in the script, so that I can access them all separately? Here's how I see it: If all the cubes are stored in memory after I declare and assign the list, there must be a way to use the WHOLE list, right? Say, could it be possible to, in the code, dynamically create variables based on the findings of the array or list? Then what I wanna do is "Debug.Log("Found cubes: " + cubes);" and by "cubes" ( know it doesn't work this way, just trying to explain what I mean) I mean "Show me the Debug.Log of each cubes (being it cubes1, cubes2, cubes3) that you can find", meaning no foreach looping. Please, help me, what am I missing? I also think that, possibly running code like: Debug.Log("Found cubes: " + cubes[0]); Debug.Log("Found cubes: " + cubes[1]); Debug.Log("Found cubes: " + cubes[2]); Debug.Log("Found cubes: " + cubes[3]); is the same as looping through them, since code still runs line by line. Is that so? That'd mean I don't have to avoid looping.
20 replies