Video Games Development

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

Unity 3D Game Engine – JS – Camera – Swap Multi Cameras – On Click

Unity 3D Game Engine – JS – Camera – Swap Multi Cameras – On Click

1. Create in the Hierarchy

– CameraTarget1 (Empty object)
– CameraTarget2 (Empty object)

– Main Camera> attach ‘CameraControl.js’

CameraControl.js:


#pragma strict

// ########################################################################################
// This complete script can be attached to a camera to change his Position and his Target
// You will move the camera by clicking GUI.Button CAMERA 1 and GUI.Button CAMERA 2
// ########################################################################################

// The relative speed at which the camera will catch up
// Small value = more steps to reach the new position
public var smooth : float = 1.5f;  

// Camera positions START ##################################################################
public var newPosCam1 : Vector3;   // Camera Position 1 
public var newPosCam2 : Vector3;   // Camera Position 2 
// Camera positions END ####################################################################

// Camera targets 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 target1 : Transform; 
var target2 : Transform;
// Camera targets END ######################################################################

function Start () {

}


function Update() {
	
}

// NOTICE: function OnGUI is outside others functions
// Move the Camera START ###################################################################	
function OnGUI () {
    // Button color and opacity setup START ################################################
    // 0.0f rende il bottone completamente trasparente, il tocco funziona ugualmente
    GUI.color = new Color(1,1,1,0.5f); 
    // Button color and opacity setup END ##################################################
    
    // Insert 8 pixels of space between the 2 buttons.
	GUILayout.Space (8);

   if (GUI.Button (Rect (600,10,200,200), "CAMERA 1")) {
        transform.position = Vector3.Lerp(transform.position, newPosCam1, smooth * Time.deltaTime);
        transform.LookAt(target2);
    }
   if (GUI.Button (Rect (600,250,200,200), "CAMERA 2")) {
        transform.position = Vector3.Lerp(transform.position, newPosCam2, smooth * Time.deltaTime);
        transform.LookAt(target1);
    }
}
// Move the Camera END #####################################################################

Main Camera> CameraControl.js:

– var Smoot: Small value = more steps to reach the new position

– New Pos Cam 1: first position of camera
– New Pos Cam 2: second position of camera

– Target 1: target of first position -> DRAG AND DROP from Hierarchy to Inspector CameraTarget1 (Empty object)
– Target 2: target of second position -> DRAG AND DROP from Hierarchy to Inspector CameraTarget2 (Empty object)

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JS – Camera – Swap Multi Cameras – On Click

Unity 3D Game Engine – Get Mouse – Draw Line

Unity 3D Game Engine – Get Mouse – Draw Line

1. Main Camera attach the script LineMouse.js

LineMouse.js


#pragma strict

// It automatically adds Add Component> Effects> Line Renderer
@script RequireComponent(LineRenderer)

var lineRenderer : LineRenderer;
var myPoints : Vector3[];

function Start () {
    // Setup of LineRenderer Component
    lineRenderer = GetComponent(LineRenderer);
    lineRenderer.SetWidth(0.2,0.2);
}

function Update () {

    if(myPoints){

        lineRenderer.SetVertexCount(myPoints.Length);
        for(var i = 0;i<myPoints.Length;i++){
            lineRenderer.SetPosition(i,myPoints[i]);    
        }
    }
    else
    lineRenderer.SetVertexCount(0);
    
    if(Input.GetMouseButtonDown(0)){
        // it is only drawing 10 points per second, as per the line
        InvokeRepeating("AddPoint",.1,.1);
    } 
     if(Input.GetMouseButtonUp(0)){
        CancelInvoke();
        myPoints = null;
    }

}

function AddPoint(){

   Debug.Log("Add");

    var tempPoints : Vector3[];

    if(!myPoints)
        tempPoints = new Vector3[1];
    else{
        tempPoints = new Vector3[myPoints.Length+1];
       
    for(var j = 0; j < myPoints.Length; j++)
        tempPoints[j] = myPoints[j];
    
   }   
     var tempPos : Vector3 = Input.mousePosition;
    tempPos.z = 10;
    
    tempPoints[j] = camera.ScreenToWorldPoint(tempPos);
   myPoints = new Vector3[tempPoints.Length];
   for(j=0; j< myPoints.Length; j++) 
   myPoints[j] = tempPoints[j];
}

NOTICE: it seems to be a little sluggish.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Get Mouse – Draw Line