Community-based video game design and development.

Archive for the ‘Scripts’ Category

C# Script: POV Camera

Engine: Unity
Language: C#

Description:
This script is a very blunt and simple approach to have the camera always 
reset to the player's current position, giving you a POV feeling. 
It precedes that you want to keep the camera separate from the 
player's avatar in the hierarchy and that you attach the script 
to the camera.
Note that you will have to make adjustments depending on how tall 
your player avatar is, as the default position may vary 
from object to object.

//Script start
public class POVCamera : MonoBehaviour {
//Initialise members
Vector3 playerposition;
Transform self;
GameObject player;

//This happens when the scene loads
void Start() {
//Returns the Transform component of the camera
self = GetComponent<Transform>(); 
//Returns the GameObject that you called "player" 
//for your player's avatar
player = GameObject.Find("player");
}

//This happens once per frame
void Update() {
//Gets the player's position in (x, y, z)
playerposition = player.transform.position;
//Sets the camera's position to the position of the player
self.transform.position = playerposition;
}
}