Unity 3D – Mouse Drag to Rotate GameObject

Unity 3D – Drag to Rotate GameObject

1. Create a Cube, assign RotateObject.js:


var clickPos 	: Vector2;
var offsetPos	: Vector2;
var divider 	= 80;

function Start()
{
	clickPos = Vector2(0,0);
	offsetPos = Vector2(0,0);
}

function Update () {

	offsetPos = Vector2(0,0);
	
	if(Input.GetKeyDown(leftClick()))
	{
		clickPos = mouseXY();
	}
	
	if(Input.GetKey(leftClick()))
	{
		offsetPos = clickPos - mouseXY();
	}
	
	// Rotate the GameObject
	transform.Rotate(Vector3(-(offsetPos.y/divider),offsetPos.x/divider,0.0), Space.World);
}

// Debug Code: Prints the current mouse position
function OnGUI ()
{
	/*GUI.Label(Rect(10,350,200,100), "mouse X = " + Input.mousePosition.x);
	GUI.Label(Rect(10,370,200,100), "mouse Y = " + Input.mousePosition.y);
	
	GUI.Label(Rect(120,350,200,100), "click X = " + clickPos.x);
	GUI.Label(Rect(120,370,200,100), "click Y = " + clickPos.y);
	
	GUI.Label(Rect(210,350,200,100), "offset X = " + offsetPos.x);
	GUI.Label(Rect(210,370,200,100), "offset Y = " + offsetPos.y);*/
}

// Return true when left mouse is clicked or hold
function leftClick()
{
	return KeyCode.Mouse0;
}

//Immediate location of the mouse
function mouseXY()
{
	return Vector2(Input.mousePosition.x, Input.mousePosition.y);
}

//Immediate location of the mouse's X coordinate
function mouseX()
{
	return Input.mousePosition.x;
}

//Immediate location of the mouse's Y coordinate
function mouseY()
{
	return Input.mousePosition.y;
}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Mouse Drag to Rotate GameObject

Unity 3D Game Engine – Camera Facing Billboard

Unity 3D Game Engine – Camera Facing Billboard

This is useful for billboards which should always face the camera and be the same way up as it is.

1. Hierarchy create the structure:

– Main Camera

– Empty Object (parent)
– Plane (child)

2. Rotate Plane to face the Main Camera

3. Select Empty Object and assign CameraFacingBillboard.JS:


#pragma strict

var m_Camera : Camera ; // Assign in Inspector

function Start () {

}

function Update () {
	transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
			                 m_Camera.transform.rotation * Vector3.up);

}

4. Hierarchy> DRAG AND DROP Main Camera over Inspector> CameraFacingBillboard.JS, Camera var

5. Run to see the final result

Original Article: http://wiki.unity3d.com/index.php?title=CameraFacingBillboard

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

Unity 3D Game Engine – JS – Basics – GameObjects – Get

Unity 3D Game Engine – JS – Basics – GameObjects – Get

A good practice is to check the properties of external GameObjects using JS.
Now, using a script inside an Empty Object we will drive other GameObjects.

Follow this steps:

1. store the GameObject inside a variable: var myobject : GameObject;
2. DRAG AND DROP from Hierarchy to Inspector var slot the object
3. Code it:
varname.property = ….
varname.component.property = …

See the JavaScript:

var myobject : GameObject; // ASSIGN IN INSPECTOR!!!!!!!

function Update () {

// NOTICE: varname.property = ....
myobject.transform.Rotate(Vector3.back, 30f * Time.deltaTime);

// NOTICE: varname.component.property = ....
myobject.renderer.material.color.a = 0.5; // set transparency to 50%


}

See API documentation at: http://docs.unity3d.com/ScriptReference/index.html

Let’s go on!

0. Project window> RMB> Import New Asset:

– Audio Clip

1. MAIN TOP MENU> GameObject> Create:

– Main Camera

– Point Light

– GUIText

– Cube
-> Transform
-> Mesh Filter
-> Box Collider
-> Mesh Renderer> add ‘material1’ (Transparent/Diffuse Shader)
-> Particle System

2. Create Empty Object, name it ‘Game Controller’
-> Audio Source without audio clip
->’GameController.JS’


#pragma strict

// create var to get some external GameObjects START #################
// ASSIGN IN INSPECTOR
var mytext : GUIText;
var myobject : GameObject;
var mycamera : Camera;
var mylight : Light;
var myaudio : AudioClip;
// create var to get some external GameObjects END ###################

function Start () {

	// instantiate objects at x=0 y=0 z=0, Quaternion.identity-> it means no rotation
	// You can use a prefab
	// Start() will be executed only once -> it will create only one clone
	Instantiate (myobject, new Vector3(0f,0f,0f), Quaternion.identity);

	// play audio clip at volume 0.7
        audio.PlayOneShot(myaudio, 0.7);
}

function Update () {

// change the property .text of GUIText
// NOTICE: varname.property = ....
mytext.text = "Change this properties using scripts!";

// rotate game object
// NOTICE: varname.property = ....
myobject.transform.Rotate(Vector3.back, 30f * Time.deltaTime);
// translate game object
myobject.transform.Translate(new Vector2(1,0)* 1f *Time.deltaTime);
// set transparency
// NOTICE: varname.component.property = ....
myobject.renderer.material.color.a = 0.5; // set transparency to 50%
// set particle system start color 
myobject.particleSystem.startColor = Color.cyan;
// disable collider
myobject.collider.enabled = false;

// change light color
mylight.light.color = Color.red;

// change camera skybox color
mycamera.camera.backgroundColor = Color.green;

} // end Update() #######################################################


3. Hierarchy DRAG AND DROP:

– Main Camera over Inspector mycamera variable
– Point Light over Inspector mylight variable
– Gui Text over Inspector mytext variable
– Cube over Inspector myobject variable
– Audio Clip over Inspector myaudio variable

4. Play

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JS – Basics – GameObjects – Get

Unity3D Game Engine – GUI Text – New Line Break

Unity3D Game Engine – GUI Text – New Line Break

New Line Break inside Inspector

1. MAIN TOP MENU> GameObject> Create Other> GUI Text
2. Open Windows Note Pad> Type your text and indent it> select it and press CTRL+C (Copy)
3. Hierarchy> select GUI Text> Inspector> GUIText> Text slot, CTRL+V (Paste)

New Line Break via JS Script

1. MAIN TOP MENU> GameObject> Create Other> GUI Text
2. Hierarchy> select GUI Text> Inspector> ‘Add Component’> New Script> MyText.JS

#pragma strict

function Start () {

}

function Update () {
guiText.text = "This is a \n line break";
}

Other supported characters:

  \n	New line
  \t	Tab
  \v    Vertical Tab
  \b	Backspace
  \r	Carriage return
  \f	Formfeed
  \\	Backslash
  \'	Single quotation mark
  \"	Double quotation mark
  \d	Octal
  \xd	Hexadecimal
  \ud	Unicode character
By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – GUI Text – New Line Break

Unity 3D Game Engine – Custom Bitmap Fonts

Unity 3D Game Engine – Custom Bitmap Fonts

Inside Unity3D When dealing with text in a project, there are 2 choices: GUIText and TextMeshes.

a. MAIN TOP MENU> GameObject> Create Other> GUIText is a HUD (head-up display) element drawn on top of everything else, it is a vector text.
It use only standard Fonts

b. MAIN TOP MENU> GameObject> Create Other> 3D Text is a real 3D Text with bitmaps mapped over some planes.
It can use custom bitmap fonts made with Photoshop

Create a Text Mesh and style it

1. MAIN TOP MENU> GameObject> Create Other> 3D Text
2. Inspector> Text Mesh> Text> use html tags, examples:

This is <b>bold</b>
Nested <b>tags <i>are</i> aloowed</b> yeah!
We are <color=green>green</color> with envy
We are <size=50>largely</size> unaffected

Official docs at http://docs.unity3d.com/Manual/StyledText.html

Remove bad low res pixel effect

1. Creare Text Mesh with a large size: Score Text
2. Reduce it with Transform Tool

Import New Fonts

Unity 3D supports .ttf .dfont .otf files.

1. MAIN TOP MENU> Assets> Import New Asset…> YourTruetype.ttf, you will see the font into Project window
Unity 3D will create: – Font Name
– Font Material (color + texture)
– Font Texture (if you type a word, a bitmap texture of letters will be created here)

2. Hierarchy> select> your 3D Text> from Project DRAG AND DROP the font over Text Mesh> Font

Dynamic Fonts

1. Select the Font> Inspector> Character> Dynamic / Unicode / ASCII default set / ASCII lower case / ASCII upper case / Custom Set

When you set the Characters drop-down in the Import Settings to Dynamic, Unity will not pre-generate a texture with all font characters. Instead, it will use the FreeType font rendering engine to create the texture on the fly.

This has the advantage that it can save in download size and texture memory, especially when you are using a font which is commonly included in user systems, so you don’t have to include the font data, or when you need to support asian languages or large font sizes (which would make the font textures very large using normal font textures).

Convert Dynamic Fonts for iOS/Android

iOS/Android device does NOT support Dynamic font, which means you cannot adjust font’s size, style in TextMesh component or GUIText component.

1. MAIN TOP MENU> Assets> Import New Asset…> YourTruetype.ttf
Unity 3D will create: – Font Name
– Font Material (color + texture)
– Font Texture (if you type a word, a bitmap texture of letters will be created here)

2. Select Font Size: 32 -> maggiore è la dimensione del font, maggiore sarà la dimensione della texture finale del punto 3.
Font Name> Character> Unicode or ASCII

3. Select Font Texture> Unapplied Import Setting> ‘Apply’> it creates a texture 128x128px or over with ALL CHARACTERS, they are already packed.

4. Drag the font into TextMesh, 3D Text or GUIText component.
For TextMesh and 3D Text don’t forget to drag font material (you can find it when collapse font asset) into MeshRender.

5. In TextMesh, 3D Text or GUIText, set Font Size to 0, Font Style to Normal. The font size and style will be determined by the font asset.
Build to your device.

Official Docs at:

– http://docs.unity3d.com/Manual/class-Font.html

– http://wiki.etc.cmu.edu/unity3d/index.php/Unity_font_issue_on_iOS/Android

Create Custom TTF Fonts

You can create your own TTF Font using a software as ‘Font Forge’ at http://sourceforge.net/projects/fontforge/files/latest/download?source=files

Create an editable texture from Dynamic Fonts

1. MAIN TOP MENU> Assets> Import New Asset…> YourTruetype.ttf
Unity 3D will create: – Font Name
– Font Material (color + texture)
– Font Texture (if you type a word, a bitmap texture of letters will be created here)

2. Select Font Size: 32 -> maggiore è la dimensione del font, maggiore sarà la dimensione della texture finale del punto 3.
Font Name> Character> Unicode or ASCII

3. Select Font Name> Inspector> LMB over TOP RIGHT small gear icon> Create Editable Copy
It will create a bitmap font with:
– Font Name_copy -> con le coordinate di ritaglio delle singole lettere
– Font Material_copy -> il materiale
– Font Texture_copy -> png con la mappa alfa

4. Open your Project Folder/Assets/Texture_copy.png> open and edit it with Photoshop.
Now you can create Font_new_color.png, with color and alpha in the same file.
A working sample here:

Font_new_color.png

3D Text

5. MAIN TOP MENU> GameObject> Create Other> 3D Text

6. Hierarchy> 3D Text> Inspector
> Mesh Renderer> Materials> DRAG AND DROP Font Material_copy, è il materiale di mapping
> TextMesh> Font> DRAG AND DROP Font Name_copy, è la mappa di ritaglio in forma testuale per identificare le lettere

7. Project> Font Material_copy> Inspector> Shader: Unlit/Transparent -> Texture DRAG ANd DROP Font_new_color.png

8. :) DONE!!!!!!!!!!!!

GUI Text

5. MAIN TOP MENU> GameObject> Create Other> GUI Text

6. Hierarchy> GUI Text> Inspector
> Mesh Renderer> Font> DRAG AND DROP Font Name_copy

7. :) DONE!!!!!!!!!!!!

NOTICE that at the end YOU CAN NOT STYLE TEXT with:

This is <b>bold</b>
Nested <b>tags <i>are</i> aloowed</b> yeah!
We are <color=green>green</color> with envy
We are <size=50>largely</size> unaffected

Convert TTF Fonts to Bitmap Fonts

Bitmap fonts, which consist of a Texture atlas (atlante) containing all of the required letters from the font, and a text data file describing the location and size of each letter on that texture atlas.

Download the software ‘Hiero’ at https://code.google.com/p/libgdx/downloads/detail?name=hiero.jar

Open Hiero
1. Font> File> load your TTF
2. Rendering setup Page Width and Page Height
3. Padding
4. File> Export

The result will be 2 files:
1.myfont.fnt -> a text file with the atlas -> rename myfont.txt
2.myfont.png -> a bitmap image font -> edit with Photoshop if you want

Use Bitmap Fonts inside Unity 3D

1. MAIN TOP MENU> Assets> Import New Asset…> Import myfont.txt and myfont.png
2. Project Window> RMB> Create> Material, name it myfontBMP
3. Project Window> RMB> Create> Custom Font, name it myfontBMP
4. Select the Material> assign the texture myfont.png
5. Follow the instruction inside this tutorial: http://sysmagazine.com/posts/212587/

Creation of bitmap fonts in Unity is very uneasy :(
In my opinion there are a lot time-saving plugins inside the Asset Store to easily manage custom font…

Custom Fonts Plugins

Text Box
https://www.assetstore.unity3d.com/en/#!/content/2877

SpriteText / Custom Bitmap Fonts
https://www.assetstore.unity3d.com/en/#!/content/3095

Advance Text Animations Plugins

TextFX
It is a text animation plugin for the Unity 3D game engine.
It is really time saving, see in action at: http://www.codeimmunity.co.uk/TextFx/index.php

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Custom Bitmap Fonts