Hi everyone,
I’m encountering a significant jitter issue with Cinemachine 3.1.1 when panning the camera, and I’m hoping someone might have insights or suggestions for resolving this.
Issue Description:
I’m working on an RTS-style camera rig where the camera pans based on dragging in world space with the cursor. The way this works is by casting a ray from the cursor to a plane that represents the world. When the ray hits the plane, I store that position as the starting point. As long as the cursor is held down, I keep casting a ray each frame and calculate the difference between the new raycast hit position and the starting point. This difference is then applied to the camera’s target transform, effectively moving the camera as the world drags under the cursor.
The issue arises when the Cinemachine Position Composer’s damping is set to 0. The camera starts jittering at damping values around 0.5 or lower. When damping is at 0, the jitter becomes excessive and doesn’t stop until I release the mouse button. However, when I set the damping to 1, the jittering goes away completely, and the camera pans smoothly as expected.
Setup:
- Position Control: Cinemachine Position Composer tracks the target transform.
- The target transform is moved by my custom script for panning.
- The camera is moved relative to world coordinates, adjusting based on the cursor’s position via raycast.
- The issue occurs specifically when damping on the Composer is set to 0.
What I’ve Tried:
- Playing around with different update methods. Changed Brain Update Method and my script update method (setting to the same, different etc.)
- Ensuring that the world movement logic is correct and smooth by applying constant movement via WASD.
- Increasing damping resolves the issue, but I’d prefer to have 0 damping for a more immediate response during panning.
- If i do a screen-based pan it does not jitter.
Demo Project:
I’ve attached a Unity package containing a simplified test scene, the camera rig, and the scripts I’m using. You can download it here: camera drag test.unitypackage (5.0 KB)
Import this to a project that has Cinemachine 3.1.1 installed in a at east 2023 editor.
Or … just use this Demo Project: WorldDragTest.zip (36.1 KB)
Used Script:
This is also included in the demo project.
using UnityEngine;
namespace DefaultNamespace
{
public class WorldDragTest : MonoBehaviour
{
public Camera Camera;
public Transform Target;
private Vector3 _startPoint;
private Vector3 _initialCameraPosition;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (GetWorldPlanePoint(Input.mousePosition, out var point))
{
_startPoint = point;
_initialCameraPosition = Target.position;
}
}
if (Input.GetMouseButton(0))
{
if (GetWorldPlanePoint(Input.mousePosition, out var point))
{
var delta = _startPoint - point;
var targetPos = _initialCameraPosition + delta;
Target.position = targetPos;
}
}
}
private bool GetWorldPlanePoint(Vector2 screenPosition, out Vector3 point)
{
var ray = Camera.ScreenPointToRay(screenPosition);
var plane = new Plane(Vector3.up, -Vector3.Dot(Vector3.Normalize(Vector3.up), -Camera.transform.position));
if (plane.Raycast(ray, out var distance))
{
point = ray.GetPoint(distance);
return true;
}
point = Vector3.zero;
return false;
}
}
}
I think the issue is because i use Camera.ScreenPointToRay. But how it is done correctly?
What I want is to have a RTS Camera using damping with any values and drag by world space not screen space.
Any help or suggestions on why this might be happening would be greatly appreciated! Thanks in advance!