Physics2D lag on increased timescale
I have been working on an infinite runner kind of game in which i am trying to implement a speedup powerup kind of thing . Something that will allow me to boost through the levels at a high speed
I did this by increasing the timescale value to 5.0 but doing that causes physics lag spikes in the profiler for mid end phones(LG - NEXUS 4)
i have quite a few colliers in the scene which i cannot reduce as the game kind of requires that collision accuracy
The impact is kind of unexpected as the areas with low physics2d actually have more active bodies at that time
is there anything i can do to reduce this , or any other way i can achieve this ?
Unity Version - 5.0.2f1
You could just let the player move faster, without changing the timescale.
i cant cause its not actually the player that moves down, its a sequence of randomly generated levels that move upward . and i cant seem to change their velocity because for some reason doing so causes them to go out of alignment and makes them overlap each other
Post the code for that. Then I could help you speed things up.
The 2D physics is reporting 0.7ms for the step time (Box2D update) but the CPU graph is showing 7.9ms for 'physics'. Double-check in the breakdown what this is and ensure that it's not 3D physics doing something odd (ignore the fact that you may not be using it).
Either way, the 2D physics is showing very little in the above image.
BTW: You'll get terrible accuracy when you increases the time-scale like that. Assu$$anonymous$$g the defaults, that'll be 50/5 = 10Hz physics updates.
i checked there's no mention of Physics in the profiler , even the physics profiler graphs shows straight zero lines in all the categories and i don't even want the collisions to be registered during this interval , its just a speed dash ignoring all the collision is there a way i can temporarily disable physics ?
You say 'Physics' but there's two sections for physics for both 2D and 3D . The graph above certainly isn't zero. If the spike is related to physics, there'll be a breakdown of what is causing that in the profiler.
You should see a complete breakdown in the CPU area.
Answer by Oribow · Apr 10, 2016 at 09:41 PM
This scripts should be much more efficient. It controlls the entire movement and spawning of the level pieces.
using UnityEngine;
//Author: Oribow
public class EndlessRunner : MonoBehaviour {
//Controlls the speed
public float runningSpeed = 1;
//Controlls the direction
public Vector3 runningDir = Vector3.left;
//Just for testing purpose
public GameObject prefab;
//Controlls the y height of the spawned objects
public Transform levelSpawnPos;
private float outOfCameraLeft;
private float outOfCameraRight;
private Camera mainCamera;
private LevelPiece firstPiece;
private LevelPiece lastPiece;
void Start()
{
mainCamera = Camera.main;
outOfCameraLeft = mainCamera.ScreenToWorldPoint(new Vector3(mainCamera.rect.xMin * Screen.width,0,0)).x;
outOfCameraRight = mainCamera.ScreenToWorldPoint(new Vector3(mainCamera.rect.xMax * Screen.width, 0,0)).x;
SpawnTheScreenFull();
}
void Update()
{
if (firstPiece.PosHorizontalRight < outOfCameraLeft)
{
LevelPiece oldPiece = firstPiece;
Destroy(oldPiece.piece);
SpawnTheScreenFull();
firstPiece = oldPiece.next;
oldPiece.next = null;
}
UpdateLevePiecePositions();
}
public void SpawnTheScreenFull()
{
Vector3 position = levelSpawnPos.position;
if (lastPiece == null)
position.x = outOfCameraLeft;
else
position.x = lastPiece.PosHorizontalRight;
while (position.x < outOfCameraRight)
{
LevelPiece currentLevelPiece = new LevelPiece(GetRandomLevelPiece());
position.x += currentLevelPiece.HalfWidth;
currentLevelPiece.piece = Instantiate(currentLevelPiece.piece, position, Quaternion.identity) as GameObject;
AddNewLevelPiece(currentLevelPiece);
position.x += currentLevelPiece.HalfWidth;
}
}
public GameObject GetRandomLevelPiece()
{
//Insert your random selector here
return prefab;
}
public void AddNewLevelPiece(LevelPiece newPiece)
{
if (lastPiece == null)
{
lastPiece = newPiece;
firstPiece = newPiece;
}
else
{
lastPiece.next = newPiece;
lastPiece = newPiece;
}
}
public void UpdateLevePiecePositions()
{
LevelPiece currentPiece = firstPiece;
while (currentPiece != null)
{
currentPiece.piece.transform.Translate(runningDir * runningSpeed * Time.deltaTime);
currentPiece = currentPiece.next;
}
}
}
LevelPiece:
using UnityEngine;
//Author: Oribow
public class LevelPiece
{
public LevelPiece next;
public GameObject piece;
private Sprite sprite;
public float PosHorizontalRight { get { return piece.transform.position.x + HalfWidth; } }
public float PosHorizontalLeft { get { return piece.transform.position.x - HalfWidth; } }
public LevelPiece(GameObject piece)
{
this.piece = piece;
sprite = piece.GetComponent<SpriteRenderer>().sprite;
next = null;
}
public float HalfWidth {
get { return (piece.transform.localScale.x * sprite.bounds.size.x) / 2; }
}
public float Width
{
get { return piece.transform.localScale.x * sprite.bounds.size.x; }
}
}
sorry for late reply i got a bit busy with life . @Oribow i tried your code , a bit modified version of it as my game is going up rather than left, and yes the physics2d performance was reduced by a lot :D but now i have some device.present and (surprisingly) physX stutters in the graphics even though it shows in the profiler there are zero bodies in the physics 3d area and i am not even using 3D physics @$$anonymous$$elv$$anonymous$$ay you were right I don't know why that is happening .
I just spoke to the 3D guys and various fixes are on the way for the case where 'Physics.Processing' is occuring when there are no 3D physics objects in the scene. I believe one of the fixes relates to thread-stalls and the other is simply where the physics update is not called at all if there are no physics objects.
So i guess i can just wait on that then :/ . but is there anything i can do to reduce the device.present impact cause it is also pretty considerable
It's typically VSync related but it's certainly the graphics driver.
Did a quick Google search: http://forum.unity3d.com/threads/device-present-in-profiler-what-the-deal.86312/