Week 4 – Dynamic Camera Movement Code

The variables are also explained.

// DynamicCamera version 0.1 28/2/2017

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DynamicCamera: MonoBehaviour {

public Transform _target;
// The target is the object that the camera will follow along

public float _smooth = 5f;
// Assures that the transition of the camera will be smooth when moving. A value change will ensure that the camera is either transitioning better or it is rawer

Vector3 offset;
// Represents how far the camera will be from the object when following it

// Use this for initialization
void Start () {

offset = transform.position – _target.position;
// Calculates the offset by subtracting the position of the object from the position of the camera. In short, it will keep the distance the camera is put at initially

}

// Update is called once per frame
void FixedUpdate () {
// Using FixedUpdate as the input and player are using the same function, as to ensure continuity

Vector3 _targetCamPosition = _target.position + offset;
// Detects where the target is and moves the camera to it, while keeping the distance set on it

//Lerps allow you to move from one value to another
transform.position = Vector3.Lerp (transform.position, _targetCamPosition, _smooth * Time.deltaTime);
// Slowly changes the position of the camera to that of the place of the target, by making use of the smooth variable, as the camera will not move fast at each transition
}
}

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