- Home /
Slightly Scrolling Camera
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?
Answer by EHogger · Feb 04, 2013 at 01:32 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092127im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera follow mouse, remain inside bounds? 0 Answers
Having issues with a smooth camera follow 2 Answers
Smooth Follow Rotation 3 Answers
Character Runs Off Camera 2 Answers
Fixed camera on rails. 2 Answers