Movement Smoothing Script not Working

I’ve created the following movement smoothing script:

using UnityEngine;
using System.Collections;

public class SmoothedCamera : MonoBehaviour {

	public Transform Following;
	public float positionSpeed = 3f;
	public float rotationSpeed = 3f;
	private float startTime = 0f;
	private float journeyLength = 0f;
	private float rotationLength = 0f;
	void Start() {
		startTime = Time.deltaTime;
		journeyLength = Vector3.Distance(transform.position, new Vector3(Following.position.x + 5, Following.position.y + 5, Following.position.z));
		rotationLength = Vector3.Distance(transform.rotation.eulerAngles, new Vector3(Following.rotation.x + 25, Following.rotation.y + 270, Following.position.z));

	void Update()
		float distCovered = (Time.deltaTime - startTime) * positionSpeed;
		float fracJourney = distCovered / journeyLength;
		if (transform.position != Following.position)
			transform.Translate(Vector3.Lerp(transform.position, new Vector3(Following.position.x + 5, Following.position.y + 5, Following.position.z), fracJourney));

		float rotCovered = (Time.deltaTime - startTime) * rotationSpeed;
		float fracRot = rotCovered / rotationLength;

		if (transform.rotation != Following.rotation)
			transform.Rotate(Quaternion.Slerp(transform.rotation, new Quaternion(Following.rotation.x + 25, Following.rotation.y + 270, Following.position.z, Following.rotation.w), fracRot).eulerAngles);

This class is supposed to smoothly translate the camera its attatched to based on a Parent Transform. This parent transform will be assigned to an empty object that is at the center of a moving vehicle.

The reason i add (5, 5, 0) to the position and (25, 270, 0) to the rotation is so that the camera will float above and behind the vehicle while still looking at it.

The camera doesn’t do anything and the error console is filled with hundreds of messages.

What did i do wrong?

It doesn’t really make much sense to put a Lerp inside Translate, or a Slerp inside Rotate. Do one or the other, not both.

Hmm in mine I use Time.time not Time.deltaTime not sure if that could cause any problems for you.

I think you will also need a different way to collect the time. Using only the start time to subtract from the current time seems like it would only work for the original movement. I believe you will need to grab a new startTime every time the player moves and use that start time until the camera reaches your desired position. Otherwise the % of distance traveled will get all messed up. It needs to be for the current distance traveled for any camera movement until it catches up. So don’t reset the time until camera comes to rest and player moves again.

In my version your code:
transform.Translate(Vector3.Lerp(transform.position, new Vector3(Following.position.x + 5, Following.position.y + 5, Following.position.z), fracJourney));

Would be:
transform.position = Vector3.Lerp(transform.position, new Vector3(Following.position.x + 5, Following.position.y + 5, Following.position.z), fracJourney));

Not sure if the transform.translate wouldn’t work as well as what I use transform.position. I would also convert your Following.position.x stuff to something like followingTransform.position. That will make it a bit easier to work with probably but shouldn’t change the result.

I also create a vector position for where the camera should be currently looking using the current desired look position in relation to the current -transform.position and I use .normalize here.

I then use that new vector position for a Quaternion.LookRotation.

And finally use the Quaternion.LookRotation for Quaternion.Slerp very similar to what the guy above posted.

Hopefully that helps! I’m not sure how clear that was. The problem with this kind of thing is that the way your character moves is most likely completely different than mine but I think if you can get the time thing sorted out it should work better!