Hi,
imagine a Jump’n Run with a scrolling camera. In my case this is what I want, but I don’t want the camera to follow the player exactly.
Lets just say, the player moves 10 units to the right on the screen, then the cam only has to move (slower than the player) 5 units to the right and has to stay there.
Any ideas?
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour {
public Transform target; //the target that the camera follows
public float cameraSpeed = 1 ; //the speed of the camera movement
public float distance = 5 ; //distance to stop from target
private Vector3 newPosition;
void Start () {
}
void Update () {
if (Mathf.Abs (transform.position.z - target.position.z) > distance){
newPosition = transform.position;
newPosition.z = Mathf.Lerp (transform.position.z,target.position.z - distance, Time.deltaTime * cameraSpeed);
transform.position = newPosition;
}
}
}
This is a basic camera script that will do what you’re asking. This example is moving on the z axis, but you can swap the .z for whatever direction you’re working with.
Attach this script to your camera, and assign the target, speed and distance in the inspector.