­

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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

Unity 3D Game Engine – Build – Android – Windows OS

Unity 3D Game Engine – Build – Android – Windows OS

There are some steps you must follow before you can build and run any code on your Android device.

Tested on Unity 4.3f – Win 7 64 bit – Android 4.2

1. Java JDK

Install Java JDK, if you have JRE is not enought!

This is because if you want to run Java programs, but not develop them, download the Java Runtime Environment, or JRETM, if you want to develop applications for Java, download the Java Development Kit, or JDKTM. The JDK includes the JRE, so you do not have to download both separately.

You need Java JDK 6 – Windows x86 (not 64 bit becuase Unity 4.x is a 32 bit software) or over from here:

http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

2. Android SDK

2. Download the Android SDK from:
https://developer.android.com/sdk/index.html#ExistingIDE

It includes the essential Android SDK components and a version of the Eclipse IDE with built-in ADT (Android Developer Tools) to streamline your Android app development.

a. Unzip the ZIP file and copy it wherever you want, example c:\adt-bundle-windows-x86-

NOTICE: YOU HAVE NOT TO RENAME OR MOVE THE FOLDER AFTER UNZIP IT OR YOU WILL GET AN ERROR!

b. Run SDK Manager to complete the installation, this will take a lot of minutes.

c. Installing the SDK be sure to add at least one Android platform with API level equal to or higher than 9 (Platform 2.3 or greater), the Platform Tools, and the USB drivers if you’re using Windows

– If the Android device is automatically recognized by the system you still might need to update the drivers with the ones that came with the Android SDK. This is done through the Windows Device Manager.

– If the device is not recognized automatically use the drivers from the Android SDK, or any specific drivers provided by the manufacturer. Additional info can be found here: http://developer.android.com/sdk/win-usb.html

— use the Android SDK Manager tool that is included with the Android SDK.
— Launch the Android SDK Manager by double-clicking SDK Manager.exe, at the root of your SDK directory.
— Expand Extras
— Check Google USB Driver package and click Install
— Proceed to install the package.
When done, the driver files are downloaded into the \extras\google\usb_driver\ directory

—————————–
OR YOU CAN USE ANDROID STUDIO
—————————–

2. Android Studio

2. Download the Android Studio Bundle from:
http://developer.android.com/sdk/installing/studio.html#Updating

a. Launch the downloaded EXE file, android-studio-bundle-.exe

b. In step 4 of Installing the SDK be sure to add at least one Android platform with API level equal to or higher than 9 (Platform 2.3 or greater), the Platform Tools, and the USB drivers if you’re using Windows.

– If the Android device is automatically recognized by the system you still might need to update the drivers with the ones that came with the Android SDK. This is done through the Windows Device Manager.

– If the device is not recognized automatically use the drivers from the Android SDK, or any specific drivers provided by the manufacturer. Additional info can be found here: http://developer.android.com/sdk/win-usb.html

It will be installed at:
Windows: \Users\\AppData\Local\Android\android-studio\sdk\

c. Setup Windows variables

On some Windows systems, the launcher script does not find where Java is installed. If you encounter this problem, you need to set an environment variable indicating the correct location.

Select Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and add a new system variable JAVA_HOME that points to your JDK folder, for example C:\Program Files\Java\jdk1.7.0_21.

3. Android Device

a. Turn on “USB Debugging” on your device.
Go to Settings -> Developer options, then enable USB debugging. As of Android Jelly Bean 4.2 the Developer options are hidden by default. To enable them tap on Settings -> About Phone -> Build Version multiple times. Then you will be able to access the Settings -> Developer options.

b. Install drivers

– Connect your Android-powered device to your computer’s USB port.
– Right-click on Computer from your desktop or Windows Explorer, and select Manage.
– Select Devices in the left pane.
– Locate and expand Other device in the right pane.
– Right-click the device name (such as Nexus S) and select Update Driver Software. This will launch the Hardware Update Wizard.
– Select Browse my computer for driver software and click Next.
– Click Browse and locate the USB driver folder. (The Google USB Driver is located in \extras\google\usb_driver\.)
– Click Next to install the driver.

4. Unity

Unity will ask to locate the folder where you installed the Android SDK, setup at:
Unity MAIN TOP MENU > Edit> Preferences> External Tools> Android SDK Location

MAIN TOP MENU> File> Buil Settings…> Platform> Android> ‘Switch platform’

MAIN TOP MENU> File> Buil Settings…> ‘Player Settings’> Inspector> setup parameters

MAIN TOP MENU> File> Buil Settings…> ‘Build’

Official Unity docs:
https://docs.unity3d.com/Documentation/Manual/android-sdksetup.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Build – Android – Windows OS