Breaking News

Unity 3D

Unity 3D

1. Start with Unity 3D:
Launch Unity 3D. Log in to your account or work "offline" (but if you want to use Unity 3D asset store for 3D scripts or models, you must go through your account).
- Click on "NEW", choose 3D and the destination of your project.
- save the current scene (File> Save Scenes):
* a 3D scene contains a hierarchy of objects (GameObject) with their parameters (position, etc.). When saving a scene, the project (set of scenes) is saved automatically.
         * The project also contains the project settings (scripts, packages and the "Build").

2. The editor of Unity 3D:
 By default, you will have in the scene a camera called Main Camera and a light illuminating the scene called Directional Light.

An editor contains the following elements:

- The 3D graphic editor to directly manipulate objects of the scene.

- The tree to view the objects that are in the scene.

- The inspector viewing the scripts, also allows manipulating the parameters.
- The explorer allows you to drag and drop the project components into the scene.
- Buttons for the 3D execution of the editor to test and debug the scene.
- The visualization according to the positioning of the virtual camera executes automatically when the scene is launched.
- A console displaying error messages.
The buttons of the graphic editor can be used as follows:

3. Create your scene:

- create an application containing for example cubes.
- create a floor by inserting a plan. (GameObject> 3D Object> Plane).
- use the inspector to help you set this map to coordinates (0, 0, 0).
- Check that it is infinitely visible when taking the view of the camera by enlarging this plane on the scale for a length and width of 100 meters.
- add a cube (GameObject> 3D Object> Cube) so that it is visible to the camera.
- give a color to your cube (Assets> Create> Material) by creating a new object (name it).
- drag and drop the material onto the cube in the tree of the scene. The cube will become blue.
- add a texture (* .png) on ​​the map. (Assets> Import New Asset ...), then drag and drop it into the scene tree.
- select the scene plan and modify the parameters to obtain the desired texture taking into account the number of repetitions of the texture "tiling".

- simulate the execution of the scene:

- Give the cube a physical behavior (Component> Physics> Rigidbody). See the result.
- drag and drop the cube to the project explorer: a "Prefab" will be created from the cube that will be renamed, for example, CubePrefab.
- Conversely, other cubes will be inserted in the image of this model in the scene (11 for example).
- execute the scene.
- select "CubePrefab" and change the Rigidbody settings to change the physical behavior.
- execute the scene with different parameters.

4. Animation of an object:
some scripts allow giving objects a behavior. We will create a C # script to run the previously created cubes.
To create a script, go to (Assets> Create> C # Script), name it RotateCube.
- when performing the scene, you must at each stage, update. (If you use Mono auto-completion, you can see the functions applicable to this transform attribute and thus find the function to perform a rotation.
- drag the script to the CubePrefab on the prefab icon. The script will appear in the inspector.
- run the scene now.
- for more genericity, add a public attribute in the RotateCube class before this function  Start() : public float speed = 3.0f;

- modify the script.
- after the appearance of the attribute in the inspector, the speed of rotation can be modified without editing the script.
-                      Note that 3.0f is a default value for a script added to a new object.

5. Interaction Management:
Unity 3D lets you capture basic inputs like mouse actions or keys on the keyboard. Unity 3D offers an Input Manager System. If you want more details in this topic, copy the following URL into your browser:

You can edit the script to enable and disable rotation. If the user presses "r", the cubes will spin.
If it presses the "r" key again, the cubes will no longer spin.
You can do this by going to (Edit> Project Setting> Input), and add a new axis by changing the size. Name it "ActivateRotation" and set the "r" key as Positive Button.
You can display a message in the editor console when the "r" key is pressed using the following formula: Debug.Log ("the message");

6. Interaction with the mouse: we will use the OnMouse ... functions of the parent class MonoBehaviour:

-  void OnMouseDown ()

-  void OnMouseDrag ()

-  void OnMouseEnter ()

-  void OnMouseExit ()

-  void OnMouseOver ()

-  void OnMouseUp ()

- Create a C # script. Name it MouseInteraction.
- Create two Rigidbody and Renderer attributes to store the RigidBody links and the Renderer of the object.
- for the Start () function, initialize the attributes using the functions:
GetComponent () and GetComponent ()
- to push the cube, use AddForce (Vector3D val_dir_force). The Camera.main attribute shows the camera but Camera.main.transform.forward gives the vector shows the front of the camera.
- to change the color, access the .material attribute on the Renderer and the .color attribute on the material: rend.material.color.

7. Navigation by keyboard/mouse interaction scripts:
- go to (Assets> Import Package> Characters).
- add FPSController in the tree.
- disable the Main Camera.
- change the position of the FPS Controller to have the desired plan.
- execute the scene, test the controls and the sound produced.

8. Dynamic creation of objects in the 3D scene:
 the Instantiate function (GameObject obj) makes it possible to instantiate a copy of the object passed in a parameter which can already be present in the scene or a prefab.
We can make sure that the yellow cubes are launched when the user makes a right-click of the mouse:
- Let's create a C # script named CreateOnClick.
- add the script to FirstPersonCharacter.
- create a public attribute GameObject to Copy to store a reference on the object to be instantiated. We can initialize the attribute by dragging and dropping the CubePrefab into the Inspector for the FirstPersonCharacter.
- By default, the right-click gives "Fire2" that can be captured with get button. Also allows instantiating a cube with the function
 Instantiate (GameObject obj). We can also change the color and place it in front of the FirstPersonCharacter, otherwise there will be a collision with the FirstPersonCharacter.
- One must apply a force for launching forward.
- the removal of objects from the scene is possible thanks to the Destroy function (GameObject obj).

9. Load a particular geometry:
 we take a car as a 3D model with textures:
- we start by downloading the .zip file, then uncompress it. its .obj describes the mesh well, while the .mtl describes the colors, textures, and texture images .tga.
- Drag and drop the entire folder into the project explorer.
- drag and drop of the model in the tree of the scene. As you can change the size and position.
- there may be some compatibility issues when loading new 3D models. To do this, you have to look for the material that corresponds to the missing elements and modify its Rendering Mode, the alpha component of the color of the Main Maps.
Thus, we must find solutions to many problems that you may encounter when creating new video games via Unity.

10. Creating an executable to view it in full screen:
       - File> Build Settings ...
       - choose between PC, Mac & Linux or WebGL.
       - Click on Build.
       - run the executable.
       - Click on Player Settings and explore its settings.
       - add properties or animations to the scene (particles, collisions ...).
Try Unity and have fun!