unity3d

Unity – Spaceship Shooting Game – JS – Asteroid

Unity – Spaceship Shooting Game – JS – Asteroid

1. Hierarchy> Asteroid (Empty Object)
– prop_asteroid_01, (3d model of Asteroid), child of Asteroid

Random Rotate

2.Hierarchy select Asteroid and assign this scripts:

RandomRotator.js

#pragma strict

// Ruzzolare
var tumble : float; // setup this variable inside Inspector

function Start () : void {
                             // Random.insideUnitSphere function give a random Vector 3 value
    rigidbody.angularVelocity = Random.insideUnitSphere * tumble; 
}

NOTICE: Random.insideUnitSphere function give a random Vector 3 value

Destroy

DestroyByContact.js

#pragma strict

// Destruction of asteroid START
function OnTriggerEnter(other : Collider) 
{
    // If there is a GameObject with Tag Boundary ignore it
    if (other.tag == "Boundary")
    {
        // Return and does not reach - Destroy - commands lines
        return;
    }
    // Else it destroies other GameObject and Itself
    Destroy(other.gameObject);
    Destroy(gameObject);
}
// Destruction of asteroid END

Destroy Advanced

DestroyByContact.js

#pragma strict

// Destruction of asteroid START
var explosion : GameObject;       // Assign GameObject inside Inspector
var playerExplosion : GameObject; // Assign GameObject inside Inspector

function OnTriggerEnter(other : Collider) 
{
    // If there is a GameObject with Tag Boundary ignore it
    if (other.tag == "Boundary")
    {
        // Return and does not reach - Destroy - commands lines
        return;
    }
    // Play explosion 
    Instantiate(explosion, transform.position, transform.rotation);
    // If there is a GameObject with Tag Player Play playerExplosion
    if (other.tag == "Player")
    {
        Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    }
    // Destroy other GameObject and Itself
    Destroy(other.gameObject);
    Destroy(gameObject);
}
// Destruction of asteroid END

Move

Mover.js

#pragma strict

var speed : float; // Assign this inside Inspector

function Start () : void {
    // forward is the Z Axis
    rigidbody.velocity = transform.forward * speed;
}

Prefab the asteroid

The Asteroid is ready to become a Prefab

1. Hierarchy> DRAG AND DROP ‘Asteroid’ inside Assets> ‘Prefab’ folder
2. Hierarchy> Asteroid> CANC to delete it

By |Unity3D, Video Games Development|Commenti disabilitati su Unity – Spaceship Shooting Game – JS – Asteroid

Unity – Spaceship Shooting Game – JS – Laser Bolt

Unity – Spaceship Shooting Game – JS – Laser Bolt

1. Create the Prefab ‘Bolt’
2. The prefabs needs inside Inspector:
– Transform
– Rigid Body
– Capsule Collider: check ‘Is Trigger’

– Mover.js

#pragma strict

var speed : float; // Assign this inside Inspector

function Start () : void {
    // forward is the Z Axis
    rigidbody.velocity = transform.forward * speed;
}

Inspector> Mover (Script)
– Speed: 20

3. Create a Prefab ‘Player’ (it is the spaceship)
4. Create an Empty Object ‘Shot Spawn’ (produttore di spari)
5. DRAG AND DROP ‘Shot Spawn’ over Hierarchy> ‘Player’, now ‘Shot Spawn’ is child of ‘Player’
6. DRAG AND DROP ‘Bolt Prefab’ over Hierarchy> ‘Shot Spawn’, now ‘Bolt Prefab’ is child of ‘Shot Spawn’

At the end the Hierarchy will be:

Player
– Shot Spawn
– Bolt Prefab

Assign to ‘Player’ PlayerController.js:

#pragma strict

class Boundary
{
    // Theese variables are public to make the code easy-reusable
    // You can setup theese variables from Inspector
    var xMin : float;
    var xMax : float;
    var zMin : float;
    var zMax : float;
}

var speed : float;
var tilt : float;
var boundary : Boundary;

var shot : GameObject;     // Inspector -> assign Bolt Prefab
var shotSpawn : Transform; // Inspector -> assign Shot Spawn Empty Object
var fireRate : float;      // Inspector -> 0.25 (seconds) = 4 shot per second

private var nextFire : float;

function Update () {
    // Get Input Fire button
    if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
        nextFire = Time.time + fireRate;
        // Return the clone of the GameObject, position, rotation if user click Fire button 
        Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
        // Assign an audio file inside Inspector
        audio.Play ();
    }
}

function FixedUpdate () {
     // Get User Input START
     var moveHorizontal : float= Input.GetAxis ("Horizontal");
     var moveVertical : float= Input.GetAxis ("Vertical");
     // Get User Input END

     // Move the GameObject
     var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rigidbody.velocity = movement * speed;

    // Limitate movement inside the screen START
    rigidbody.position = new Vector3 
    (
        Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
        0.0f, 
        Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );
    // Limitate movement inside the screen END

    // Tilt movement START
    rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    // Tilt movement END
}

Hierarchy> Player> Shot Spawn> Bolt CANC to delete
Se non si cancella questo oggetto, all’inizio del gioco la nostra astronave sparerà subito un colpo, invece noi vogliamo che non vengano emessi colpi fino a che il tasto fire resta inutilizzato.

7. MAIN tOP MENU> GameObject> Cube and name it ‘Boundary’
8. Hierarchy> select ‘Cube’> Inspector>
– Transform> small gear icon> Reset
– Mesh Render> uncheck
– Box Collider> check ‘Is Trigger’
9. Scale the box to surround all your game area

10. Create and assign to ‘Boundary’ DestroyByBoundary.js

#pragma strict

// Destroy Game Objects START
function OnTriggerExit(other : Collider)
{
    // Destroy all GameObjects WITH COLLIDER that run away THIS Collider
    // The GameObject WITHOUT COLLIDER will not be detroyed!!!
    Destroy(other.gameObject);
}
// Destroy Game Objects END

11. Hierarchy> ‘Boundary’> Inspector> Remove ‘Mesh Filter”Mesh Renderer’

By |Unity3D, Video Games Development|Commenti disabilitati su Unity – Spaceship Shooting Game – JS – Laser Bolt

Unity – Spaceship Shooting Game – JS – Spaceship

Unity – Spaceship Shooting Game – JS – Spaceship

1. Hierarchy> select the Space Ship GameObject> Inspector> ‘Add Component’> Scripts> JS Script

The code of ‘PlayerController.js’:

#pragma strict

class Boundary
{
    // Theese variables are public to make the code easy-reusable
    // You can setup theese variables from Inspector
    var xMin : float;
    var xMax : float;
    var zMin : float;
    var zMax : float;
}

// Theese variables are public to make the code easy-reusable
// You can setup theese variables from Inspector
var speed : float;
var tilt : float;
var boundary : Boundary;

function FixedUpdate () {
     // Get User Input START
     var moveHorizontal : float= Input.GetAxis ("Horizontal");
     var moveVertical : float= Input.GetAxis ("Vertical");
     // Get User Input END

     var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rigidbody.velocity = movement * speed;

    // Limitate movement inside the screen START
    rigidbody.position = new Vector3 
    (
        Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
        0.0f, 
        Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );
     // Limitate movement inside the screen END

    // Tilt movement START
    rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    // Tilt movement END
}

2. Hierarchy> select the Space Ship GameObject> Inspector> ‘PlayerController.js’

– Speed
– Tilt

Boundary
– XMin: to get this value you can go to ‘Game’ view, select ‘Space Ship’ GameObject> Inspector> Transform> change X Position

– XMax: to get this value you can go to ‘Game’ view, select ‘Space Ship’ GameObject> Inspector> Transform> change X Position

– ZMin: to get this value you can go to ‘Game’ view, select ‘Space Ship’ GameObject> Inspector> Transform> change Z Position

– ZMax: to get this value you can go to ‘Game’ view, select ‘Space Ship’ GameObject> Inspector> Transform> change Z Position

By |Unity3D, Video Games Development|Commenti disabilitati su Unity – Spaceship Shooting Game – JS – Spaceship

3DS MAX – Biped to Unity Game Engine

3DS MAX – Biped to Unity Game Engine

Rigging a character using Biped and Skin in 3DS Max, animate and export it to Unity Game Engine.

Scene Setup

1. Setup the units using the parameters in the link below:

http://www.lucedigitale.com/blog/3ds-max-micro-reference-manual-unit-setup-interiors/

2. Create and animate a mesh with Biped + Skin Modifier.
You have not to use ‘Physique Modifier’ because ‘Skin Modifier’ has best support in FBX format.

3. Select the mesh and give it a name, this name will be the name of ‘Mesh’ component inside Unity

FBX Export

4. Select the mesh> MAIN TOP LEFT BUTTON> File> Export> Export Selected> FBX> give it a name, it will be the name of the Asset inside Unity.

FBX Export setup:

>Geometry
check TurboSmooth
check Convert Deforming Dummies to Bones
check Preserve edge orientation

>Animation
check Animation

>Deformations
check Deformations
check Skins
check Morphs

>Unroll Rotations
check Follow Y-Up

>Scale Factor
check Automatic (1 unity = 1 3DSMax meter)

>Axis Conversion
Up Axis = Y-up

>FBX File Format
Type: Binary
Version: FBX 2013

Unity – Import

1. Open Unity> MAIN TOP MENU> Assets> Import New Asset…> select the .FBX file

2. Project> the new Asset (the blue box icon) with the .FBX file name> DRAG AND DROP into Viewport the blue box icon

You will see:

Viewport>

1 unity = 1 3DSMax meter

Project>

Asset blue box icon (file name)
-> Inspector> Rig> Animation Type: Generic> ‘Apply’ button
-> Model> Scale Factor: 0.01 (the original 3DSMax scene was in centimeters), resize if you need> ‘Apply’ button
-> Animations> + or – to cut Takes, Loop Time
– Mesh
– Take 001 (the animation)
– Avatar
– Bip001
– Material

Hierarchy>

Asset (file name) -> Transform + Animator
– Bip001
— Bip001 Pelvis
— Bip001 Spine -> Transform, you can re-animate bones with the transform tools!
— …
– Mesh -> Transform + Skinned Mesh Renderer + Material

Unity – Apply Animation

1. Projects> RMB> Create> Animator Controller> name it ‘PlayerAnimator’

2. MAIN TOP MENU> Window> Animator

3. Project> Take 001 DRAG AND DROP into Animator window, ‘Take 001’ turn in yellow because it is the default animation

4. Project> Animator Controller ‘PlayerAnimator’ DRAG AND DROP into Hierarchy> Asset (file name)> Animator> Controller

5. MAIN TOP BUTTON ‘Play’

Unity – Multiple Takes

Solution one

1. Inside 3DSMax you can create multiple animations inside timeline, example

a. frame 0-25 walk
b. frame 26-50 run
c. frame 51-100 die

2. File> Export Selected> FBX

FBX Export setup:

>Bake Animation
check Bake Animation
Start: 0 – End: 25

Save as walk.fbx

3. Unity> MAIN TOP MENU> Assets> Import New Asset…> walk.fbx

Project> you will find the Take001 animation of 1-25

Repeat the operation for all the takes you need.

Solution two

1. Inside 3DSMax you can create multiple animations inside timeline, example

a. frame 0-25 walk
b. frame 26-50 run
c. frame 51-100 die

2. File> Export Selected> FBX

FBX Export setup:

>Bake Animation
check Bake Animation
Start: 0 – End: 100

Save as walk.fbx, alla animations will be stored in a single file.

3. Unity> MAIN TOP MENU> Assets> Import New Asset…> walk.fbx

Project> select the new Asset (blue box icon)> Inspector> Animations> Clips use + or – to cut the Take using the procedure below:

a. + icon
b. Give the name
c. setup ‘Start’ ‘End’
d. ‘Apply’ button

Repeat the operation for all the takes you need.

Project> you will find the Take001 animation of 1-25 – Take002 etc…

By |Unity3D, Video Games Development|Commenti disabilitati su 3DS MAX – Biped to Unity Game Engine

Unity – Performance Test

Unity – Performance Test

Sometimes you need a benchmarks and system performance tests.

1. File> Open Project…> open your project
2. MAIN TOP MENU> Window> Profiler
3. MAIN TOP Play button to run the game

Now you can see inside Profiler Window:
– CPU Usage
– GPU Usage
– Rendering
– Memory
– Audio
– Physics
– Physics (2D)

By |Unity3D, Video Games Development|Commenti disabilitati su Unity – Performance Test