Procedural camera (head) movement

I’m experimenting in creating a natural head movement of a human at standstill in first person perspective. I’ve googled around for any data on this topic without much luck, I’m not sure about what search terms might work.

Anyway, I have created a basic working prototype where the cameras position is slow rotated between random points, it seems a bit jerky despite the lerp and other than reducing the lerp speed further and the maximum / minimum rotation I’m unsure how I can make it seem more natural.

I’d appreciate any input on this and here is the code for anyone that wants to get this up and running in unity and play with it (in Unityscript).

Just attach this script to your main camera and you are off.

#pragma strict

var fromRot: Transform;
var targetRotation = Quaternion.Euler (1.0, 0, 1.0);

function Start () {	
}
 
function Update() {

	var lerpSpeed = 0.005f;
	var fromRot = gameObject.transform; //get cameras current rotation
				
	// transition camera until we reach targetRotation
	if (fromRot.rotation != targetRotation)
	{
		transform.rotation = Quaternion.Lerp(fromRot.rotation, targetRotation, Time.time * lerpSpeed);
	}
	else{
		//generate new random target
		targetRotation = Quaternion.Euler (Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
	}
	
}

Thanks in advance!

1 Answer

1

I tried out your script, but made a few minor changes.

#pragma strict
 
var fromRot: Transform;
var targetRotation = Quaternion.Euler (10.0, 0, 10.0);
 
function Start () {    
}
 
function LateUpdate() {
 
    var lerpSpeed = 1.0f;
    var fromRot = gameObject.transform; //get cameras current rotation
 
    // transition camera until we reach targetRotation
    if (fromRot.rotation != targetRotation)
    {
        transform.rotation = Quaternion.Lerp(fromRot.rotation, targetRotation, Time.deltaTime * lerpSpeed);
    }
    else{
        //generate new random target
        targetRotation = Quaternion.Euler (Random.Range(-10.0, 10.0), 0, Random.Range(-10.0, 10.0));
    }
 
}

Firstly, I moved the script to LateUpdate. Can’t really explain why other than most camera scripts I’ve found almost always use LateUpdate to move things. Secondly, I switched out the Time.time in your Lerp to Time.deltaTime. Time.time is literally the seconds that have passed since the application has loaded. This means that the longer the application has been playing, the faster your speed is. Time.deltaTime only counts the time since the last frame.

Other than that, I just increased the distances of the target rotations to 10. Try it out and see if it works like you wanted.