Unity 3D: Spectator Camera Like in Call of Duty

Like the title says we are going to create a spectator camera view. For those of you who don’t know what I’m taking about just watch this video and understand the goooodness 🙂

Ok let’s start

1) Open Unity (Duh…)

2) Create a C# Script File and name it Spectator.cs

3) Paste the following code

using UnityEngine;
using System.Collections;

public class Spectator : MonoBehaviour {

 //initial speed
 public int speed =20;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {

 //press shift to move faster
 if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
 {
    speed = 40; 
 
 }
 else
 {
    //if shift is not pressed, reset to default speed
    speed =20; 
 }
 //For the following 'if statements' don't include 'else if', so that the user can press multiple buttons at the same time
 //move camera to the left
 if(Input.GetKey(KeyCode.A))
 {
    transform.position = transform.position + Camera.main.transform.right *-1 * speed * Time.deltaTime;
 }

 //move camera backwards
 if(Input.GetKey(KeyCode.S))
 {
    transform.position = transform.position + Camera.main.transform.forward *-1 * speed * Time.deltaTime;
 
 }
 //move camera to the right
 if(Input.GetKey(KeyCode.D))
 {
    transform.position = transform.position + Camera.main.transform.right * speed * Time.deltaTime;
 
 }
 //move camera forward
 if(Input.GetKey(KeyCode.W))
 {
 
    transform.position = transform.position + Camera.main.transform.forward * speed * Time.deltaTime;
 }
 //move camera upwards
 if(Input.GetKey(KeyCode.E))
 {
    transform.position = transform.position + Camera.main.transform.up * speed * Time.deltaTime;
 }
 //move camera downwards
 if(Input.GetKey(KeyCode.Q))
 {
    transform.position = transform.position + Camera.main.transform.up * -1 *speed * Time.deltaTime;
 }

 }
}

4) Attach the Spectator.cs to the main camera

5) Attach the Mouse Look Script (comes as a ready component with Unity) to the main camera

Final Result:

Spectator

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s