Video Games Development

Unity3D – AddComponent – Javascript

Unity3D – AddComponent – Javascript

AddComponent to THIS GameObject

Create a Cube and attach:


#pragma strict

function Start() {
        // Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = gameObject.AddComponent ("SphereCollider");
	}
	
function Update(){
	}

Play and watch Cube params into Inspector

AddComponent to ANOTHER GameObject

Create a Cube

Create an EmptyObject and attach:


#pragma strict

private var cubeobj : GameObject;

function Start() {
	cubeobj = GameObject.Find("/Cube"); // Find gameobject with name

        // Adds the sphere collider to the game object
	var sc : SphereCollider;
	sc = cubeobj.AddComponent ("SphereCollider");
	}
	
function Update(){
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – AddComponent – Javascript

Unity3D – Position of a GameObject – JavaScript

Unity3D – Position of a GameObject – JavaScript


        // Move the object to (0, 0, 0)
	transform.position = Vector3(0, 0, 0);
		
	// Print the x component of the position to the Console
	print(transform.position.x);

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Position of a GameObject – JavaScript

Unity3D – Game Engine – Get Material Name

Unity3D – Game Engine – Get Material Name

Get the material of THIS gameobject.
IMPORTANT: material is not a Component but a class of renderer Component, the correct statement is:

...
// GameObject.Component.Class.Property
this.renderer.material.name
...

Create a Cube, assign a material named ‘material0’, assign the script:


#pragma strict

function Start () {

}

function OnMouseDown ()
{
        // When you click over the object play audio
        audio.Play();
        
        // Check material
        // NB: il materiale non è un componente ma una classe di renderer!!!!
        var myMaterial = renderer.material;
        Debug.Log(myMaterial); // -> Risulta: material0 (Instance)
        
    if (this.renderer.material.name == "material0 (Instance)") {
        Debug.Log("material0 found!");
    } else {
        Debug.Log("material0 not found!");
    }
}

function Update () {

}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Game Engine – Get Material Name

Unity3D Game Engine – Materials Array

Unity3D Game Engine – Materials Array

Array of Materials

Create a scene with:

– Main Camera

– 3 Materials blue-green-red

– Cube, assign the script SwitchMaterials.js:


#pragma strict
 
var materials : Material[];
var materials_counter : int = 0; // 1. Setup this in Inspector! It setup the array lenght!
                                 // 2. DRAG AND DROP materials inside slots
function Update() 
{
    if(Input.GetKeyDown("f")) // press f key to switch materials
    {
        renderer.material = materials[GetNextMaterialIndex()]; // richiama l'indice del materiale da visualizzare
                                                               // risulta -> render.material = materials[0]
    }
}
 
function GetNextMaterialIndex() : int
{
    materials_counter++; // incrementa di 1 l'indice
    if (materials_counter==materials.Length)
    {
        materials_counter = 0; // se l'indice è già uguale alla lunghezza massima riporta il valore a 0
    }
    return materials_counter; // ritorna l'indice dell'array
}

Cube> Inspector> SwitchMaterials.js>
1. Dare un valore a Materials Size (è la lunghezza dell’indice)
2. DRAG AND DROP i materiali sugli slot Element 0-1-2 etc…

Play> press f to switch materials.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – Materials Array

Unity3D – Game Engine – Artificial Intelligence

Unity3D – Game Engine – Artificial Intelligence

What is AI?

Artificial Intelligence (AI) is essentially the subject of making computers able to think and decide like living organisms to perform specific operations. AI in videogame is the brain of NPC (Non Player Characters), the computer-driven characters.

AI setup

Our NPC opponents can be very stupid like a zombie or super intelligence as the chess game champion Kasparow but the important thing it is our game have to be funny! The NPC can not be invicible or too easy to win. Finding the right challenge level is the key to make a game fun to play.
There are a lot of techniques, so let’s go on!

Finite State Machines

It is the the simplest AI model.
A state machine basically consists of a finite number of states that are connected in a graph by the transitions between them.

NPC character -> It follows some rules -> It changes state

NPC character can only be in exactly one state at any given time.

Random and Probability

In videogames, randomness and probabilities are applied in the decision making process of AI calculations.

Stupid NPC
-> good way (30%) -> it is easy to beat it!
-> bad way (70%)

Genius NPC
-> good way (90%) -> run fast and escape!
-> bad way (10%)

Sensor System

Our NPCs need eyes and ears to know about enemies and world they are interacting with.
The basic informations the NPCs need could vary a lot depending on the type of the game, for example:

NPC in a RTS (Real Time Startegy) game:
– Position of the player
– Buildings and objects nearby
– Player’s health and its own health
– Location of resources on the map

NPC zombie:
– Position of the player (to eat him…) :)

Pooling

One method to collect the informations of the world is polling.
NPCs -> Pooling (if/else) -> Reaction

We can simply do if/else or switch checks in the FixedUpdate() method of our AI character.

The messaging system

NPC AI does decision making in response to the events in the world.
The messaging system provides a central checking system.

Event -> NPCs (listeners) -> Pooling -> Reaction

Flocking, swarming, and herding

Think about group of birds flocking in the sky. We can define simple rules for each bird to create the whole group behaviour.

There are 3 simple rules:
– Separation: minimum distance between neighbors
– Alignement: direction based on direction of the neighbors
– Cohesion : minimum distance with the group’s center of mass

Path following and steering

Think about a racing game, the AI opponents need to navigate on the road.

A* (star) Pathfinding

The AI units need to find a path to reach the goal without colliding with the obstacles or colliding with others NPC.
A* (pronounced “A star”) is a pathfinding algorithm widely used in games, because of its performance and accuracy.

A Navigation Mesh

A navigation mesh it is a poly mesh that uses polygons to represent the areas in the map that an AI entity can travel. It is the ‘road’ of our NPCs players. An NPC will knows more informations about world than a waypoint system as A* Pathfinding system. The AI character will knows the safe areas, if there is mud, grass, ice, water and so on…
Unity3D Pro has a built-in navigation mesh generator.

Behavior Trees

With a behavior tree you can represet the logic of an AI character.

NPC Action
|1.-> |Is Player faraway?
|Patrol (Ispeziona)
|2.-> |Do I see the Player?
|Chase (Raggiungi)
|3.-> |Is Player near enought to attack?
|Attack (Attacca)

Try to think as your NPC:
1. The Player is faraway -> Patrol |2. I see the Player -> Chase |3. I am near enough -> Attack
The NPC can not reach point 3. if do not reach point 2. first.
Point 3. it is the child behaviour of point 2.

Locomotor System

Locomotor system manages different NPC’s walk cycles to walk on plane, run, climb a ladder, attack an enemy etc…
Unity3D pro has a built-in locomotion system: Mecanim

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Game Engine – Artificial Intelligence