cursor-following object jitters when camera moves

in my scene i have an object that is projected onto the nearest surface behind the cursor with this script:

using UnityEngine;
using System.Collections;

public class CursorHit : MonoBehaviour {
	
	public HeadLookController headLook;
	
	// Update is called once per frame
	void LateUpdate () {	
		Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		if (Physics.Raycast(cursorRay, out hit)) {
			transform.position = hit.point;
		}
		
		headLook.target = transform.position;
	}
}

this is currently the script i am using to make the camera follow the player character when it moves:

using UnityEngine;
using System.Collections;

public class SmoothFollowCamera : MonoBehaviour {
	
	public GameObject target;
	public float smoothingTime = 0.5f;
	public Vector3 offset = Vector3.zero;
	public float rotateAngle = 45;
	public float rotateTime = 25;
	public bool useBounds = false;
	private SmoothFollower follower;
	
	// Use this for initialization
	void Start () {
		follower = new SmoothFollower(smoothingTime);
	}
	
	// Update is called once per frame
	void LateUpdate () {
		if (target==null) return;
		
		Vector3 targetPoint = target.transform.position;
		if (useBounds) {
			Renderer r = target.GetComponentInChildren(typeof(Renderer)) as Renderer;
			Vector3 targetPointCenter = r.bounds.center;
			if (!float.IsNaN(targetPointCenter.x)
				&targetPointCenter.magnitude < 10000) targetPoint = targetPointCenter;
		}
		transform.position = follower.Update(targetPoint + offset, Time.deltaTime);
		transform.rotation = Quaternion.AngleAxis(
			Mathf.Sin(Time.time*2*Mathf.PI/rotateTime)*rotateAngle,
			Vector3.up
		);
	}
}

it may also be worth noting that the player character faces the cursor object based on the Head Look Controller.

whenever the camera moves, the object that follows the cursor jitters. changing the scripts to use different combinations of Update and LateUpdate did not yield a satisfying, predictable solution (and as i understand it, the camera should always use LateUpdate.) what should i change to ensure smooth movement of both the cursor-following object and the camera itself?

It sounds like you really need the first script to fire after the camera has moved. It might be worth changing it’s LateUpdate method to another name that isn’t automatically called by Unity, and manually call it from your camera script at the end of it’s LateUpdate method. It’s not ideal coding practice, but sometimes you need to get a little hacky to get the job done. :wink:

yeah, not the cleanest solution i guess (if only there was an EvenLaterUpdate or something :wink: ), but it works great. thanks Murcho.

Glad to hear it works. Not being in 100% control of event execution means sometimes you have to get a bit hacky, but if it works it works :lol: