- Home /
Lag When Built With WebGL (Link for examining the problem) Updated! Please help!
Here is the link for my game so you can see if you can identify the problem:
https://chillpillgames.itch.io/flying-balls
So my project is very small (less than 10mb), but there is major lag when I build it. The game was very slow and laggy after I built it, but in the editor it was perfectly fine. I tried Itch.io and a localhost but they both had the same result. Here are my code samples if they help:
Sphere Spawner:
using UnityEngine;
public class EnemySpawnScript : MonoBehaviour
{
public GameObject enemyPrefab;
private float amountToSpawn = 2000;
private void Update()
{
if(amountToSpawn > 0)
{
Instantiate(enemyPrefab, new Vector3(Random.Range(-199, 199), Random.Range(-99, 99), 0), Quaternion.identity);
amountToSpawn -= 1f;
}
}
}
Movement:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float thrust = 1000f;
public Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetKeyUp("w"))
{
rb.AddForce(0, thrust, 0);
}
if (Input.GetKeyUp("s"))
{
rb.AddForce(0, -thrust, 0);
}
if (Input.GetKeyUp("a"))
{
rb.AddForce(-thrust, 0, 0);
}
if (Input.GetKeyUp("d"))
{
rb.AddForce(thrust, 0, 0);
}
}
}
Collision:
using UnityEngine;
using UnityEngine.UI;
public class PlayerCollisionScript : MonoBehaviour
{
public Text finalScoreText;
public Text scoreText;
private int score = 0;
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Enemy")
{
Destroy(collision.gameObject);
score += 1000;
scoreText.text = "Score: " + score.ToString();
finalScoreText.text = "Score: " + score.ToString();
SliderScript.juiceLeft += 3;
}
}
}
I really need help and also I need the demo build finished by 2 days from now, and anything you say can definitely help me. Also if you have any ideas for the game that would be great!
We can't really help you here since you haven't provided any useful information. I tested your WebGL build and yes, you have some serious input delay. Since we have no idea how you read your input, how you setup your input (input manager? sensitivity? gravity? of your inputs) or how you move your sphere around there's nothing we can base an answer on. Next thing is instantiating spheres is quite expensive and pretty unnecessary for a 2d game. A sphere has a couple of hundreds of vertices and triangles.
Try some of my webGL examples. I don't really have games up there but just some examples. However those which have direct keyboard input are my VisPortals test and my simple towers of hanoi game (keys 1,2 and 3). You probably are using the physics engine for your collision detection. While this is possible, using the Physics2D engine and proper 2d colliders would be most likely better for performance. Though I don't think that rendering performance is an actual issue here. We need to see the code that actually moves your sphere.
Note that if you plan to have that many "spheres" / dots on the screen you might want to use Unity particle system. I made this example years ago. It simply reads in an image with the unity logo and creates a particle for each non transparent pixel. When you hold down the mouse button you create a repulsive spherical force around your mouse. The particles are forced back to their resting position every frame. Note that I have 23000+ particles in that example. Doing this with gameobjects would completely kill the game.
$$anonymous$$eep in $$anonymous$$d that WebGL generally has very little memory available. By default only 256$$anonymous$$B. A gameobject is quite large and rather expensive if you have many of them.
Please edit your question and add relevant details about your game. Especially your input settings / input method and the code that actually moves your sphere.
I did 3d because I don’t know how to work 2d physics, collision, movement, and layers and it would take me long to figure out. This project is just something I want to start with so that I can build off of later. The memory of my entire game is less than 10mb so I don’t see how that could be a problem. I don’t know how to work with a particle system and it’s not something that I want to spend an hour to learn. I know the basics but not the things that you do.
@Bunny83 I updated the question. Hope this helps!
Answer by Bunny83 · Feb 04, 2020 at 02:13 AM
Now that we see your code it's pretty obvious what is wrong. The biggest issue is that you use "GetKeyUp". This will only be true for one frame when you release your key. So pressing down one of your buttons will have no effect at all. However whenever you release one of your keys you apply a one-time force.
If you really want your control to be one-time impulses so the user has to constantly tapping the movement buttons, you should use GetKeyDown instead of GetKeyUp. This will react to a key down event instead of a key up event.
Though if you want to accelerate into the direction while the user presses a key down, you should use GetKey instead of GetKeyDown / Up.
Finally you use AddForce wrong. AddForce with ForceMode.Force (the default) expects a force in Newton which should be applied every frame over time. If you want to apply a one-time impulse you should use ForceMode.Impulse. Though if you use GetKey and you apply a force every frame while a button is pressed down, you should use ForceMode.Force. Note that your force value need to be much smaller. Currently you would apply 1000 Newtons (1000 kg*m/s²). So a mass of 1kg would be accelerated to 1000 m/s every second. A value of about 20 would make more sense. Keep in mind since you made your force variable public, changing the field initializer in your code from 1000 to 20 has no effect since the value is serialized in the inspector. So you have to change the value in the inspector.
// impulse logic as explained above
private void Update()
{
if (Input.GetKeyDown("w"))
rb.AddForce(0, thrust, 0, ForceMode.Impulse);
if (Input.GetKeyDown("s"))
rb.AddForce(0, -thrust, 0, ForceMode.Impulse);
if (Input.GetKeyDown("a"))
rb.AddForce(-thrust, 0, 0, ForceMode.Impulse);
if (Input.GetKeyDown("d"))
rb.AddForce(thrust, 0, 0, ForceMode.Impulse);
}
If you want to accelerate continuously use something like that:
private void FixedUpdate()
{
if (Input.GetKey("w"))
rb.AddForce(0, thrust, 0);
if (Input.GetKey("s"))
rb.AddForce(0, -thrust, 0);
if (Input.GetKey("a"))
rb.AddForce(-thrust, 0, 0);
if (Input.GetKey("d"))
rb.AddForce(thrust, 0, 0);
}
Answer by Nk_Khumbhani · Feb 03, 2020 at 11:55 AM
I think you should use invokeRepeating to spawn spheres in parts or use object pulling concept.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to reduce Draw-Calls? 1 Answer
WebGL Build bugged Colliders 0 Answers
Gun not smoothly following player,How to make gun smoothly follow player 1 Answer