How to get a smooth movement of GameObjects
Hi, All. I created simple 2D project. In the editor it works fine. But when I run it on devices (Android, iOS), it slows down periodically. Movement is not smooth, objects jerk. In android FPS is 60, but periodically it drops to 40 and game freezing. Unity 5.3.3f1 Personal. Project is very simple: 1 prefab with Rigidbody2d and 3 scripts, but I can't understand the problem. Can somebody help?
CreateGame.cs:
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CreateGame : MonoBehaviour {
public GameObject cube;
private List<GameObject> listCubes;
private int CUBES_COUNT = 40;
// Use this for initialization
void Start () {
//Create Object Pool
createPool ();
//Get Cube from Pool
Invoke ("CreateCube", 0f);
}
//Create Pool
void createPool(){
for (int i = 0; i < CUBES_COUNT; i++) {
ObjPool.instance.cubes.putObj((GameObject)Instantiate(cube));
}
}
void CreateCube()
{
//get Cube from pool
ObjPool.instance.cubes.getObj ();
//Invoke itself
Invoke("CreateCube", UnityEngine.Random.Range (0.2f, 0.5f));
}
// Update is called once per frame
void Update () {
//get Android back button
if (Application.platform == RuntimePlatform.Android) {if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); }}
}
}
ObjectPool.cs:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class ObjPool : MonoBehaviour {
public static ObjPool instance { get; private set; }
public class Pool {
private List<GameObject> pool;
public Pool(){
pool = new List<GameObject>();
}
public void putObj (GameObject obj){
obj.SetActive (false);
pool.Add (obj);
}
public GameObject getObj(){
if (pool.Count > 0)
{
GameObject pooledObject = pool[0];
pool.RemoveAt(0);
pooledObject.transform.parent = null;
pooledObject.SetActive(true);
return pooledObject;
}
return null;
}
}
public Pool cubes;
void OnEnable()
{
instance = this;
cubes = new Pool ();
}
}
MoveCube.cs: using UnityEngine; using System.Collections;
public class MoveCube : MonoBehaviour {
private float range = 4f;
private Vector2 velocity;
private Vector3 initPosition;
private Rigidbody2D rigidBody2d;
void Start () {
rigidBody2d = GetComponent<Rigidbody2D> ();
}
void OnEnable(){
Init ();
}
public void Init()
{
if (initPosition == Vector3.zero) {
initPosition = transform.position;
}
transform.position = new Vector3(initPosition.x, initPosition.y - range * Random.value, initPosition.z);
transform.eulerAngles = Vector3.zero;
transform.localScale = new Vector3(1, 1, 1);
}
void FixedUpdate()
{
velocity = new Vector2(-10, 0);
rigidBody2d.velocity = velocity;
}
// Update is called once per frame
void Update () {
// Die by being out off screen
if (transform.position.x < -13)
{
ObjPool.instance.cubes.putObj(gameObject);
}
}
}
Unity 5.5.3 personal doesn't exist yet: only pro users get beta versions ;)
Your answer
Follow this Question
Related Questions
My Camera rotation is lagging in completed Game 0 Answers
CAMERA FOLLOW MOVING PLAYER PROBLEM ? 0 Answers
UI Canvas Jerking Performance terrible.. 0 Answers
Jerking game when touch the screen 0 Answers