Game Bucks

Top-down Shooter Camera


1. Create a new camera or use the main camera and drag this script onto it.

2. The target should be the player (of course).

3. The ‘player’ should be a mesh renderer. (a visual representation of the player). If you don’t have a mesh renderer in your assets, then GameObject -> Create new -> Capsule. Drag the capsule into the player and center the position at (0, 0, 0). Drag the capsule into the ‘player’ variable.

4. Distance – How far away from ‘target’ based on z-axis.

5. Center Offset – offsets the center of target in a elliptical pattern. (raise the value and you will know what i mean).

6. Height – How far away from ‘target’ based on y-axis.

7. Damping – How smooth the position interpolates to target position.

8. Look Multiplier X and Y multiplies the x position and y position based on mouse position. (mouse look).

using UnityEngine;
using System.Collections;

public class PlayerCamera : MonoBehaviour {
public Transform target;
public GameObject player;
public float distance = 2.0f;
public float centerOffset = 1.0f;
public float height = 15.0f;
public float damping = 3.0f;
public float lookMultiplierX = 12;
public float lookMultiplierY = 12;

private float inputX;
private float inputY;
private float wantedHeight;

void Update() {
	if(target) {
		wantedHeight = player.transform.position.y + height;

		inputX = (Input.mousePosition.x - Screen.width * 0.5f) / Screen.width * lookMultiplierX;
		inputY = (Input.mousePosition.y - Screen.height * 0.5f) / Screen.height * lookMultiplierY;

		Vector3 targetPos = new Vector3(target.position.x + inputX, wantedHeight, target.position.z + inputY - distance) + player.transform.forward * centerOffset;
		transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * damping);
		transform.eulerAngles = new Vector3(90 - inputY, 0, 0);

Game Bucks

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.