Bad stutter on Android (specifically Samsung Galaxy S4), with very simple code
Full project: https://github.com/SixMinute/UnitySamsungS4Stutter
InGameCameraController.cs:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InGameCameraController : MonoBehaviour
{
public Transform ballTrans;
public Rigidbody2D rb;
Vector3 _CarPos;
Vector3 _CameraPrevPos;
private Camera _Camera;
float _OrthographicSizeStart;
public static float SMOOTH_CAMERA_FOLLOW_SPEED = 10f;
private float _ZoomExtra;
private Vector3 _CameraVelocity = Vector3.zero;
void Awake ()
{
_Camera = GetComponent<Camera>();
Application.targetFrameRate = -1;
}
public void LateUpdate()
{
_CarPos = getCarPosition();
updateCameraSmoothFollow();
}
public float dampTime = 0.3f;
private Vector3 velocity = Vector3.zero;
public Transform target;
void updateCameraSmoothFollow ()
{
if (target)
{
Vector3 point = _Camera.WorldToViewportPoint(target.position);
Vector3 delta = target.position - _Camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}
bool updateCamera()
{
return true;
}
Vector3 getCarPosition()
{
return ballTrans.position;
}
Vector2 getCarVelocity()
{
return rb.velocity;
}
}
MoveCamera.cs
using UnityEngine;
using System.Collections;
public class MoveCamera : MonoBehaviour {
public float _MaxY;
public float _MinY;
private bool _DirUp;
private float _CurrentY;
private Vector3 _LocalVector;
void Start ()
{
_CurrentY = _MaxY;
_DirUp = false;
_LocalVector = new Vector3();
}
void Update ()
{
float distanceThisFrame = 10.0f * Time.deltaTime;
if(_DirUp)
{
_CurrentY += distanceThisFrame;
if(_CurrentY > _MaxY)
{
_CurrentY = _MaxY;
_DirUp = false;
}
}
else
{
_CurrentY -= distanceThisFrame;
if(_CurrentY < _MinY)
{
_CurrentY = _MinY;
_DirUp = true;
}
}
_LocalVector.Set(transform.position.x, _CurrentY, transform.position.z);
transform.position = _LocalVector;
}
}
SMRect.cs:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class SMRect
{
public float minX;
public float minY;
public float maxX;
public float maxY;
public SMRect(float minX, float minY, float maxX, float maxY)
{
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}
public static SMRect fromBounds(Bounds bounds)
{
return new SMRect(bounds.min.x, bounds.min.y ,bounds.max.x, bounds.max.y);
}
}
It's an incredibly simple project, with nearly nothing happening other than a few camera changes, but stutters every 5-10 seconds on more than a few Android devices, and incredibly evident on the Samsung Galaxy S4.
This is obviously just a sample project, but it's happening in our main project, too and it's hammering our performance no matter how many other optimisations we do. So long as this stutter continues our game, and I'm assuming many other Unity games on Android, massively suffer.
There's two bug reports we've submitted about this, 702035, where we weren't able to nail down exactly what caused the stutter, nor exactly which devices show it clearly, but our newest one, from yesterday, 724610, is the clearest example of this problem we've been able to find. The bug hasn't yet, as far as I know, popped it's head up on the issue tracker (I'll update this thread when it does), but I wanted to put this here as well, in case others also get hit with this.
I'm assuming this is an internal Unity problem, but just in case others have suggestions, or potential solutions for this, they'd be really great to see.
have you checked the profiler for spikes? try deep profiling if there is and you want to know more exactly.
Yup, have profiled and found nothing like a smoking gun. Delved into quite a few Android things that popped up in Logcat, but nothing pointed to anything. One way or another, with such small code, you'd have to consider any spike in performance something that Unity must look at themselves, probably.
Your answer
![](https://koobas.hobune.stream/wayback/20220612002508im_/https://answers.unity.com/themes/thub/images/avi.jpg)