5
\$\begingroup\$

I have an object that automatically moves by itself and I want the main camera to automatically follow it.(Like in games such as geometry dash and jetpack joyride) This is the code for the automatic moving object in case it is needed:

using UnityEngine;
using System.Collections;
public class automove : MonoBehaviour {
public static int movespeed = 5;
public Vector3 userDirection = Vector3.right;



public void Update()
{
    transform.Translate(userDirection * movespeed * Time.deltaTime); 
}
}

So does anyone know any good scripts I could add to the main camera to follow this object that automatically moves? Thanks in advance!

\$\endgroup\$

3 Answers 3

10
\$\begingroup\$
  1. Make the camera object a child of the object that's moving. This is super easy and doesn't take any additional scripts.
  2. Make a script for the camera that will update its position based on a target object:

Example:

public GameObject target;
public float xOffset = 0;
public float yOffset = 0;
public float zOffset = 0;

void LateUpdate() {
    this.transform.position = new Vector3(target.transform.position.x + xOffset,
                                          target.transform.position.y + yOffset,
                                          target.transform.position.z + zOffset);
}

You'll want to do this in LateUpdate so you're sure the object you're tracking has already moved. The offsets will be something you decide for your game.

\$\endgroup\$
6
\$\begingroup\$

You cited games like "Jetpack Joyride" and "Geometry Dash" so I understand you are working on a 2D game with side scrolling view. I assume the Y is up and X is right.

In Jetpack joyride: - the camera is fixed - the player only moves along Y-Axis - you move transform the level and background to simulate that the player is moving, so the player speed is instead -enviromentSpeed. You can imagine your player on a treadmill.

In "Geometry Dash" the camera has an added grade of freedom as is smooth-following the player along Y-Axis.

You can refer to the included "Unity Script Package" from Standard Assets script present in Unity called CameraSmoothFollow2D.

Here a video: https://www.youtube.com/watch?v=MBA_r3pCtxo

The key part is the Update part where you update the Camera position to match the target position (generally the target is the main character and you want the same X and Y maintaining your current Z as Z is the distance between the player and the camera.). You generally don't want to instantaneously match his position but introduce some smoothing function or damp like this

Vector3 destination = target.transform.position;
destination.z = transform.position.z;
transform.position = Vector3.SmoothDamp(transform.position, destination, velocity, dampTime); 

you put this in a script attached to your camera and velocity and dampTime are parameters to tune the camera behaviour

\$\endgroup\$
-2
\$\begingroup\$

Hey I know this is old but I released a project on this with a 3rd person camera, fps camera and an rts camera on github. Here's the link

\$\endgroup\$
1
  • \$\begingroup\$ While your github example might answer the question, in the best case your answering the question with a link, and in the worst case, your spamming. Link-based answers are not constructive, as the answer becomes redundant if the link ever dies. Instead, summarise what you can here, to answer the question. You can still provide your link, for further reading, or as an example. \$\endgroup\$
    – Gnemlock
    Oct 2, 2016 at 3:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .