Home > Back-end >  Why does exporting to Unity WebGL change the speed of objects in my game?
Why does exporting to Unity WebGL change the speed of objects in my game?

Time:10-05

GOAL
So I have a space-invaders type game I have made to practise unity and I want to upload it to Itch.io as I have done in the past. This is a clip of the game.
I switched platforms to WebGL in Build Settings etc, and everything worked fine in Unity.

PROBLEM
However when I built it and uploaded the zip file to Itch.io, the aliens are now a lot faster than usual.
(Be aware there may be other things that have changed, I just haven't been able to get to them since it is extremely hard).

CODE

Alien movement:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AleenController : MonoBehaviour
{
    Rigidbody2D rigidbody;

    public float speed = 10f;

    ScoreController scoreController;

    AudioSource aleenDie;

    void Start()
    {
        scoreController = GameObject.Find("Canvas/Score").GetComponent<ScoreController>();
        rigidbody = GetComponent<Rigidbody2D>();
        aleenDie = GameObject.Find("Main Camera/AleenDie").GetComponent<AudioSource>();
    }

    void Update()
    {
        rigidbody.MovePosition(transform.position   new Vector3 (0, -1, 0) * speed * Time.deltaTime);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "laser")
        {
            aleenDie.Play();
            scoreController.score  = 1;
            Destroy(gameObject);
        }
    }
}

NOTE
I am really struggling to find out what is wrong here, since everything is fine in unity. If you do need more details just leave a comment.

CodePudding user response:

If you use physics you should

  • not set or get values via Transform at all
  • do things in FixedUpdate

In general though in your case instead of using

void Update()
{
    rigidbody.MovePosition(transform.position   new Vector3 (0, -1, 0) * speed * Time.deltaTime);
}

at all simply set the velocity once like e.g.

void Start ()
{
    ...

    rigidbody.velocity = Vector3.down * speed;
}

The rest seems to depend a bit on your screen size. Looks like you are moving your elements in pixel space -> the smaller the display the less pixel distance between the top and bottom -> the faster elements seem to travel.

  • Related