C# Twitchy Camera Movement

I have script that makes the main camera follow my mouse. However sometimes the main camera’s movement can be very twitchy. I used Time.smoothDeltaTime instead of Time.deltaTime but there still seems to be a bit of twitchy movement from the Main camera. What else can I do to try to smooth out this erratic movement?

using UnityEngine;
using System.Collections;

public class Camera_Move : MonoBehaviour {
	public float sensitivityX = 2F;
	public float sensitivityY = 2F;
	// Use this for initialization
	void Start () {
	
	}

	// Update is called once per frame
	void Update ()
	{
			float positionX = transform.position.x + Input.mousePosition.x * sensitivityX;
			float positionY = transform.position.y + Input.mousePosition.y * sensitivityY;
			
			transform.position = new Vector3( positionX, positionY, 0) * Time.smoothDeltaTime;
	}
}

Consider using LateUpdate?

Input.mousePosition gives pixel coordinates from the bottom left at 0/0. So to me, positionX and positionY seems like they should always be positive, and your camera should always be moving up and to the right (assuming it's looking in the positive z-direction) Is that the case? I can't see where the twitching is coming from, but using Vector3.MoveTowards together with Time.DeltaTime is usually the best way to ensure smooth movement of anything.

Seconded on using LateUpdate. http://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html "a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update."

1 Answer

1

Use Vector3.Lerp(From,To,smoothness * Time.deltaTime)

smoothness is just a float that you choose