Camera follow in C#

So I’m trying to make a game where you control a ball and try to avoid obstacles and I’m trying to make the camera follow the player but I can’t do it, I’ve tried sever scripts but none of the work and. so if you could help me make a script or make me a script would be awesome. Thanks :slight_smile:

You don’t need a script!

If the ball is rolling:

Create a child empty GameObject, and place it under the ball character. Then, take the main camera and place it in the empty GameObect.

If the ball is not rolling, place the camera under the ball!

GameObject Camera;

GameObject Ball;

public int CameraHeight;

void Update()
Camera.transform.position = new Vector3( Ball.transform.postition.x, CameraHeight , Ball.transform.postition.z );

Tell me if that worked. Greetings

something like this…

//get a vector pointing from camera towards the ball

Vector3 lookToward = ball.transform.position - camera.transform.position;

//make camera look at ball

camera.transform.forward = lookToward .Normalized();

//make it stay a fixed distance behind ball

float followDistance = 5f;

camera.transform.position = ball.transform.position - lookToward .Normalized() * followDistance;

//move camera up a bit

camera.transform.position += Vector3.up * 2f;

//this is a hard chain following the ball… if you want it smoothed… then instead of setting the camera position directly… calculate NewPos instead… then lerp towards it
newPos = same as the code above… instead of setting camera position… then…

camera.transform.position += (newPos - camera.transform.position) * k * Time.deltatime;

//where K is how quickly it catches up (float)

Couldn’t you just child the camera to the ball?

This is the camera follow that I usually like to use, idk how well it will work in your game, without seeing it.

//inside LateUpdate Function
smooth = 3;
		float xDist = target.transform.position.x - camX;
		float yDist = target.transform.position.y - camY;
		float zDist = target.transform.position.z - camZ;
                //Adjust from inspector or set here
		camZ = 2;//whatever value you like
		camX = -4;
		camY = -4f;

		//lerp to follow player
		transform.position = Vector3.Lerp (transform.position, new Vector3 (xDist, yDist, zDist), smooth * Time.deltaTime);

I found a this script in the Unity Tutorials and it works, the reason that it didn’t work before it was because I was in an older version but still Thanks to everyone that tried to help me. Here’s the script

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

public GameObject player;

private Vector3 offset;

void Start ()
    offset = transform.position - player.transform.position;

void LateUpdate ()
    transform.position = player.transform.position + offset;


@Minecraftero33 Have you tried the ‘SmoothFollow’ script in the ‘Standard Assets/Utility’, which works well for my roller ball demo.
You can add this script to your main camera, and then add the ball as the ‘Target’.

using UnityEngine;

namespace UnityStandardAssets.Utility
	public class SmoothFollow : MonoBehaviour

		// The target we are following
		private Transform target;
		// The distance in the x-z plane to the target
		private float distance = 10.0f;
		// the height we want the camera to be above the target
		private float height = 5.0f;

		private float rotationDamping;
		private float heightDamping;

		// Use this for initialization
		void Start() { }

		// Update is called once per frame
		void LateUpdate()
			// Early out if we don't have a target
			if (!target)

			// Calculate the current rotation angles
			var wantedRotationAngle = target.eulerAngles.y;
			var wantedHeight = target.position.y + height;

			var currentRotationAngle = transform.eulerAngles.y;
			var currentHeight = transform.position.y;

			// Damp the rotation around the y-axis
			currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

			// Damp the height
			currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

			// Convert the angle into a rotation
			var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);

			// Set the position of the camera on the x-z plane to:
			// distance meters behind the target
			transform.position = target.position;
			transform.position -= currentRotation * Vector3.forward * distance;

			// Set the height of the camera
			transform.position = new Vector3(transform.position.x ,currentHeight , transform.position.z);

			// Always look at the target

I am looking for this too. Did you find a script that could work?