- Home /
Game runs normal on editor, but it's slow on build
Hi, i'm making a 2d plataformer game, and, even with it being just a simple small test level with just the player and a ground, it gets really slow when build. It runs normally in the editor, and i can't find any solution to this, does anyone know how to fix it?
Answer by enzosonic04 · May 04, 2020 at 10:15 PM
Apparently it has something with Time.Deltatime, because i'm moving the player with transform.position instead with RigidBody, but i still don't know how to fix that... There is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public Animator animator;
Vector3 pos;
public Rigidbody2D rb;
//variables
public bool Ground = false;
public float xsp = 0f; //the speed in which sonic is moving horizontally
public float ysp = 0f; //the speed in which sonic is moving vertically
public float gsp = 0f; //the speed in which sonic is moving on the ground
//constants
float air = 0.0009375f;
float jmp = 20.005f;
float grv = 0.21875f;
float acc = 0.046875f;
float dec = 0.07f;
float frc = 0.046875f;
float top = 15f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rb = GetComponent<Rigidbody2D>();
#region animation
//Stop running animation when not running
if (gsp == 0)
{
animator.SetBool("Run", false);
}
//Flip When Turning
Vector2 Scale = transform.localScale;
if (Input.GetAxis("Horizontal") < 0)
{
animator.SetBool("Run", true);
Scale.x = -6;
}
if (Input.GetAxis("Horizontal") > 0)
{
animator.SetBool("Run", true);
Scale.x = 6;
}
transform.localScale = Scale;
//Animator Speed
animator.SetFloat("Speed", Mathf.Abs(gsp));
#endregion
#region Horizontal Moviment
//Left Input
if (Input.GetKey("left"))
{
if (gsp > 0)
{
gsp -= dec;
if (gsp <= 0)
{
gsp = -0.5f;
}
}
else if (gsp > -top)
{
gsp -= acc;
if (gsp <= -top)
{
gsp = -top;
}
}
}
//Right Input
if (Input.GetKey("right"))
{
if (gsp < 0)
{
gsp += dec;
if (gsp >= 0)
{
gsp = 0.5f;
}
}
else if (gsp < top)
{
gsp += acc;
if (gsp >= top)
{
gsp = top;
}
}
}
//Friction
if (!Input.GetKey("left") && !Input.GetKey("right"))
{
animator.SetBool("Run", false);
gsp -= Mathf.Min(Mathf.Abs(gsp), frc) * Mathf.Sign(gsp);
}
if (Input.GetKey("left") && Input.GetKey("right"))
{
if (gsp == 0)
{
animator.SetBool("Run", false);
}
gsp -= Mathf.Min(Mathf.Abs(gsp), frc) * Mathf.Sign(gsp);
dec = 0;
} else {
dec = 0.07f;
}
#endregion
#region Vertical Moviment
if (Ground == false)
{
ysp -= grv;
}
if (Input.GetKey("z"))
{
if (Ground)
{
Ground = false;
ysp += jmp;
}
}
#endregion
}
void FixedUpdate()
{
//Position = gsp
pos = transform.position;
pos.y += ysp * Time.fixedDeltaTime;
pos.x += gsp * Time.fixedDeltaTime;
transform.position = pos;
}
void OnCollisionEnter2D(Collision2D col)
{
ysp = 0;
Ground = true;
}
void OnCollisionExit2D(Collision2D col)
{
Ground = false;
}
}
Your answer
Follow this Question
Related Questions
Unity Build doesnt work as intended but in the editor, it works perfectly!!! 0 Answers
Sprite Shape completely incorrect in build. 0 Answers
Distribute terrain in zones 3 Answers
Why is my build running so slow despite running fine in editor? 2 Answers
(SOLVED)LoadLevel takes 3s in editor and 30s (!) after build. 0 Answers