I just realized that when i move far away from my camera start position it stutters horrible… And i honestly have no idea why … What the heck did i forget ?
public class CameraMovement : MonoBehaviour
{
[SerializeField]
float _panSpeed = 20f;
[SerializeField]
float _zoomSpeed = 50f;
[SerializeField]
Camera _referenceCamera;
Quaternion _originalRotation;
Vector3 _origin;
Vector3 _delta;
bool _shouldDrag;
void Awake()
{
_originalRotation = Quaternion.Euler(0, transform.eulerAngles.y, 0);
if (_referenceCamera == null)
{
_referenceCamera = GetComponent<Camera>();
if (_referenceCamera == null)
{
throw new System.Exception("You must have a reference camera assigned!");
}
}
}
void LateUpdate()
{
var x = 0f;
var y = 0f;
var z = 0f;
if (Input.GetMouseButton(0))
{
var mousePosition = Input.mousePosition;
mousePosition.z = _referenceCamera.transform.localPosition.y;
_delta = _referenceCamera.ScreenToWorldPoint(mousePosition) - _referenceCamera.transform.localPosition;
_delta.y = 0f;
if (_shouldDrag == false)
{
_shouldDrag = true;
_origin = _referenceCamera.ScreenToWorldPoint(mousePosition);
}
}
else
{
_shouldDrag = false;
}
if (_shouldDrag == true)
{
var offset = _origin - _delta;
offset.y = transform.localPosition.y;
transform.localPosition = offset;
}
}
}
I’m having a little trouble reading your code, but I think you might want to lerp the camera a specific distance each frame toward the mouse position, or clamp the amount of motion, or smoothdamp.
How far are you getting from the camera start location when this happens? Are you far from the scene origin? What I’m getting at is you may be encountering floating point accuracy issues if you are far from 0,0,0. In general you start to notice this type of thing if any x, y, or z value is above 10,000 or below -10,000, and even more so past 100,000.
Yeah, its not completly my code. I Took the most of it from an example. I just want to move my camera by dragging on android ^^. How hard can that be ? It works, but it stutters… and thats not good.
I already heard about it, but im about at 1000;0,1000… so that shouldnt be a problem.
No idea if that will work or not, I’ve never even used smoothdamp and I think it’s for longer distances, not update each time. You just looked so desperate pushing it like that.