Unity 3D Game Engine – Camera – 3D Platform – Smoot Follow

Unity 3D Game Engine – Camera – 3D Platform – Smoot Follow

1. Inside Hierarchy create the structure:

– Player (parent)
— CameraTarget (Empty Object) (child) -> posizionarlo in alto dietro il Player (con un valore di Z più piccolo)

– Main Camera

2. Select ‘Main Camera’ -> ruotarla leggermente verso il basso per inquadrare il player and create ‘CameraController.js’

CameraController.js


#pragma strict
 
var cameraTarget : GameObject; // Inspector> Assign the Camera Target NON è il target della camera ma la posizione che vuole raggiungere la camera

var smoothTime : float = 0.1;              // Delay in seconds to follow Player
var cameraFollowX : boolean = true;        // Inspector> if is checked -> The Camera will follow X position of cameraTarget
var cameraFollowY : boolean = true;        // Inspector> if is checked -> The Camera will follow Y position of cameraTarget
var cameraFollowZ : boolean = true;        // Inspector> if is checked -> The Camera will follow Z position of cameraTarget
var cameraFollowHeight : boolean = false;  // if true the Camera Y Position = cameraHeight
var cameraHeight : float = 2.5;            // cameraHeight
var velocity : Vector3;
private var thisTransform : Transform;    

function Start ()
{
  thisTransform = transform;
}

function Update () 
{

if (cameraFollowX) // if cameraFollowX = true = Inspector is checked
{
  thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, velocity.x, smoothTime);
}

if (cameraFollowY) // if cameraFollowY = true = Inspector is checked
{
  thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, velocity.y, smoothTime);
}

if (cameraFollowZ) // if cameraFollowZ = true = Inspector is checked
{
  thisTransform.position.z = Mathf.SmoothDamp (thisTransform.position.z, cameraTarget.transform.position.z, velocity.z, smoothTime);
}

if (!cameraFollowY && cameraFollowHeight)     // if cameraFollowY = false = Inspector is unchecked AND cameraFollowHeight = true = Inspector is checked
{
  camera.transform.position.y = cameraHeight; // The Camera Y position = cameraHeight
}

}

Main Camera> Inspector> LookAt.js> DRAG AND DROP over:
– var Camera Target -> CameraTarget (Empty Object)
– var Smoot Time -> Delay time to reach final position

a. Camera Follow X check | Camera Follow Y check | Camera Follow Height uncheck | Camera Height
-> follow X and Y of CameraTarget

b. Camera Follow X uncheck | Camera Follow Y check | Camera Follow Height uncheck | Camera Height
-> follow Y of CameraTarget the X value is the current X position in the viewport

c. Camera Follow X check | Camera Follow Y uncheck | Camera Follow Height check | Camera Height
-> follow X of CameraTarget and Y Camera Height value

d. Camera Follow Z check
-> follow Z of CameraTarget

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Camera – 3D Platform – Smoot Follow

Unity 3D Game Engine – JS Script – Follow the Player & Indipendent LookAt

Unity 3D Game Engine – JS Script – Follow the Player & Indipendent LookAt

1. Hierarchy> Create the structure:

– Player (the GameObject the Camera Group will follow)

– CameraGroup (Empty Object) -> father
— CameraTarget (Empty Object) -> child
— Main Camera -> child

2. ‘CameraGroup’ Object assign ‘CameraController.js’


#pragma strict
 
//The GameObject the Camera will follow 
public var player : GameObject;
//The actual Camera Position
private var offset :  Vector3;
 
function Start () {
offset = transform.position;
}
 
function LateUpdate () {
//LateUpdate is called after all Update functions have been called. 
// This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
// The camera moves player.position+his own position
transform.position = player.transform.position + offset;
}

Inspector> CameraGroup> LookAt.js> Assign to var GameObject-> Player Object

3. ‘Main Camera’ Object assign ‘LookAt.js’


#pragma strict

// #################################################################################################
// This complete script can be attached to a camera to make it continuously point at another object.
// ################################################################################################# 

// Camera target public variable START #############################################################
// The target variable shows up as a property in the inspector. Drag another object onto it to make the camera look at it.
var cameraTarget : Transform;
// Camera target public variable END ###############################################################

function Start () {

}
	
function Update() {
	
}
	
function LateUpdate () {
//LateUpdate is called after all Update functions have been called. 
// This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
transform.LookAt(cameraTarget);
}

Inspector> Main Camera> LookAt.js> Assign to var cameraTargett-> CameraTarget Object

4. PLAY> while playing… Inspector> change Player’s position and CameraTarget’s position

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JS Script – Follow the Player & Indipendent LookAt

Unity 3D Game Engine – JS Script – Follow the Player

Unity 3D Game Engine – JS Script – Follow the Player

1. MAIN TOP MENU> Game Object> Create Other> Cube> rename it ‘Player’

2. Hierarchy> Select ‘Main Camera’> Inspector> ‘Add Component’> New script> ‘CameraController.js’

CameraController.js:


#pragma strict
 
//The GameObject the Camera will follow 
public var player : GameObject;
//The actual Camera Position
private var offset :  Vector3;
 
function Start () {
offset = transform.position;
}
 
function LateUpdate () {
//LateUpdate is called after all Update functions have been called. 
// This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
// The camera moves player.position+his own position
transform.position = player.transform.position + offset;
}

3. Hierarchy> ‘Main Camera’> Inspector> LookAt.js> DRAG ANd DROP over puiblic variable ‘Player’> the ‘Player’ object you have created at point 1.

4. PLAY the game, while playing from Inspector change ‘Player’ object position to see the final result.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JS Script – Follow the Player

Unity 3D Game Engine – JS Script – LookAt Camera Target

Unity 3D Game Engine – JS Script – LookAt Camera Target

1. MAIN TOP MENU> Game Object> Create Empty> Inspector> rename it ‘CameraTarget’

2. Hierarchy> Select ‘Main Camera’> Inspector> ‘Add Component’> New script> ‘LookAt.js’

LookAt.js:


#pragma strict

// #################################################################################################
// This complete script can be attached to a camera to make it continuously point at another object.
// ################################################################################################# 

// Camera target public variable START #############################################################
// The target variable shows up as a property in the inspector. Drag another object onto it to make the camera look at it.
var cameraTarget : Transform;
// Camera target public variable END ###############################################################

function Start () {

}
	
function Update() {
	
}
	
function LateUpdate () {
//LateUpdate is called after all Update functions have been called. 
// This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
transform.LookAt(cameraTarget);
}

3. Hierarchy> ‘Main Camera’> Inspector> LookAt.js> DRAG ANd DROP over puiblic variable ‘Camera Target’> the ‘CameraTarget’ empty object you have created at point 1.

4. PLAY the game, while playing from Inspector change ‘CameraTarget’ object position to see the final result.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JS Script – LookAt Camera Target

Unity 3D Game Engine – Camera – Mouse Orbit

Unity 3D Game Engine – Camera – Mouse Orbit

1. Inside Hierachy create the objects:

– CameraTarget

– Main Camera, assign MouseOrbit.js

MouseOrbit.js


var target : Transform; // Inspector> Assign the LookAt Camera Target Object
var distance = 10.0;    // distance of the camera from the Target Object

var xSpeed = 250.0; // Speed of x rotation
var ySpeed = 120.0; // Speed of y rotation

var yMinLimit = -20; // y minimum rotation limit
var yMaxLimit = 80;  // y maximum rotation limit

private var x = 0.0;
private var y = 0.0;

private var smooth = 0.0;

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function LateUpdate () {
    if (target) {
    	if(Input.GetMouseButton(0))	{
        	x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        	y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
        }
 		
 		y = ClampAngle(y, yMinLimit, yMaxLimit);
 		       
        var rotation = Quaternion.Euler(y, x, 0);
        var position = rotation * Vector3(0.0, 0.5, -distance) + target.position;
        
        transform.rotation = rotation;
        transform.position = position;
    }
    
    if(Input.GetAxis("Mouse ScrollWheel"))	{
    	smooth += Input.GetAxis("Mouse ScrollWheel");
    }
    distance += smooth;
    if(distance < 1)  // la Camera non si avvicina più di 1 unità
    	distance = 1; 
    if(distance > 6) // la Camera non si allontana più di 6 unità
    	distance = 6; 
    if(smooth != 0)
    	smooth /= 1.2;

}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

Inspector> Main Camera> CameraOrbit.js> Assign the ‘CameraTarget’ Object to var target.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Camera – Mouse Orbit