Les informations ci-dessous ont été écrites sur la base d’Unity 5
Install Unity
- Download and install Unity following the installation prompts: http://unity3d.com/unity/download
- Bien vérifier (pour Android) que le SDK téléchargé est bien le dernier et que le path pour le JDK est bien mis à jour. Dans mon cas j’ai été obligé sous windows de rajouter la variable JAVA_HOME=C:\Program Files\Java\jdk1.8.0_31\bin;C:\Program Files\Java\jdk1.8.0_31;C:\Program Files\Java\jre1.8.0_31)
1°) Création d’un nouveau projet :
Project/New project/2D puis Create Project
2°) Organisation du projet :
dans la fenêtre « Project » créez (via le menu create) 3 dossiers “Prefabs”, “Scenes”, “Scripts”, and “Textures”.
Prefabs
Un Prefab est un objet pouvant être réutilisé comme des balles, des murs,…
Scenes
Une Scene est un niveau de jeu.
Scripts
Ce dossier contiendra le code du jeu.
Textures
Toutes les images utilisées dans le jeu. Dans les jeux 2D ces images sont appelées “Sprites”.
3°) Remplissage des dossiers :
a) Fond d’écran : téléchargez ici l’image de fond d’écran puis par un click and drop placez là dans le dossier texture.
Ensuite, glissez l’image dans la zone scene et modifier le paramètre scale en X=2.5 et Y=2.5
b) Ajout du joueur : téléchargez ici l’image puis déposez là dans le dossier texture. Glissez la ensuite dans la zone scene et modifier le paramétre transform à -1 ceci indiquant que cette image doit toujours être au premier plan. Ensuite In the inspector, click Add Component, type “Rigidbody 2D”, and press enter. A Rigidbody component gives the airplane gravity and other physics characteristics. Pour tester les caractéristiques allouées à l’image, appuyez sur la flèche play.
4°) Dynamique du jeux :
a) Controlling the Player
Now we’re going to create a script that allows the player to move. Inside the Scripts folder, create a C# file called Player
. Fill the contents with this code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
// The force which is added when the player jumps
// This can be changed in the Inspector window
public Vector2 jumpForce = new Vector2(0, 300);
void FixedUpdate ()
{
// Jump
if (Input.GetMouseButton(0)) //si appui sur le bouton gauche de la souris
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
}
}
}
There are two main parts in this code: the jumpForce
variable and the Update
method.
jumpForce
stores the force applied to the player when we jump.- It is of the
Vector2
type, meaning that it stores two values:x
andy
. - Because this variable is public, you can change its value in the Inspector.
- It is of the
- The
Update
method is a function that is called every frame of the game. In it, if the spacebar button is pressed, a force is added to the rigidbody.
Add this script to the Player object the same way you added the RigidBody 2D, with the Add Component button. Now when you press Play, you’ll be able to jump up and fall back down.
Attention : pour l’exécution sous Android, modifiez le script afin de détecter l’appui sur l’écran :
// Jump
//if (Input.GetKeyUp("space"))
if (Input.touchCount == 1) {
// touch on screen
if (Input.GetTouch (0).phase == TouchPhase.Began) {
GetComponent<Rigidbody2D> ().velocity = Vector2.zero;
GetComponent<Rigidbody2D> ().AddForce (jumpForce);
}
}
Référence : http://answers.unity3d.com/questions/359754/how-can-i-detect-touch-on-anroid-or-iphone.html
b) Obstacles :
Téléchargez dans le dossier Textures les images 1 et 2 placez les dans la Scene et changer leur taille X et Y en 2.5
Position these objects so that they are above each other, to the right of the background, and wide enough apart that the player can jump through them.
In the file menu, go to GameObject->Create Empty. This will add an object to the scene that is invisible and will serve as a folder that holds our rock obstacles. Name it “RockPair”. Drag the two rock GameObjects onto the RockPair object.
Add a “RigidBody 2D” component to the RockPair parent object. In the inspector, check Is Kinematic. This prevents the obstacles from being affected by gravity.
Create another script called Obstacle
and place it in the Scripts
folder. This script will be used to move the rocks from the right of the screen to the left. Fill this file with this code:
using UnityEngine;
public class Obstacle : MonoBehaviour
{
public Vector2 velocity = new Vector2(-4, 0);
// Use this for initialization
void Start()
{
rigidbody2D.velocity = velocity;
}
}
The Start
method runs once, when the GameObject is created. In this case, we are setting the object’s velocity to be -4
in the x
direction.
Add this script to the RockPair GameObject and hit the Play button. You should see the obstacles move across the screen.
Generating obstacles
We need to create new rock obstacles every few seconds. To begin, drag the RockPair Object into the Prefabs
folder. This turns RockPair into a Prefab, which is an object that can be created and destroyed many times. Delete the RockPair object that is in the scene.
In the file menu, go to GameObject->Create Empty Create another Empty GameObject and rename it to “Scripts”.
Create another Script called Generate
Paste the folowing code into it and add the Generate script to the Scripts empty GameObject.
using UnityEngine;
public class Generate : MonoBehaviour
{
public GameObject rocks;
// Use this for initialization
void Start()
{
InvokeRepeating("CreateObstacle", 1f, 1.5f);
}
void CreateObstacle()
{
Instantiate(rocks);
}
}
In this script, we use the InvokeRepeating
method. This will call a specific function once every several seconds. The first parameter is a string with the name of the method to call. The second is the number of seconds to delay these repeated calls. And the third parameter is the number of seconds between method calls.
In the CreateObstacle
method, we use Instantiate
, a method that will generate a new prefab to the scene. And the prefab that we add to the scene is a variable called rocks
. This variable isn’t linked to our RockPair prefab yet though. To do this, drag the RockPair prefab from its folder into the empty field that says rocks in the Inspector. Pour faire cela, ouvrez le dossier Assets/Prefabs, cliquez sur Scripts dans la fenêtre Hérarchie puis glissez RockPair dans la fenêtre Inspector, champ Rocks
Killing the Player
You may have noticed that running into the obstacles doesn’t do anything. Let’s make something happen because of this collision.
Click on the player GameObject. Add a component called “Box Collider 2D”.
Now go to the RockPair prefab and click on the small arrow. Select the first object and add a component called “Polygon Collider 2D”. Do the same for the other Obstacle.
A Collider component is a shape that triggers collisions to happen. Play the game and see what happens. It’s a really interesting thing to watch.
IMPORTANT :
Créer un répertoire Scenes contenant au moins une scene puis cliquez sur play
A tester :
http://www.instructables.com/id/Make-A-2D-Infinite-Runner-with-Unity/?ALLSTEPS
PlayStation :
https://www.playstation.com/en-us/develop/
publier avec unity sur le store
Références :
Excellent tutoriel : http://anwell.me/articles/unity3d-flappy-bird/
et http://noobtuts.com/unity/2d-arkanoid-game
https://www.assetstore.unity3d.com/en/#!/content/116
http://unity3d.com/learn/tutorials/projects/space-shooter
https://devs.ouya.tv/developers/docs/make_a_game_in_20_minutes
http://docs.unity3d.com/Manual/android-GettingStarted.html
http://jeux.developpez.com/videos/tutoriels/unity/13-deplacer-GameObject/
http://unity3d.com/learn/tutorials/projects/roll-a-ball/set-up?playlist=17141
http://jeux.developpez.com/videos/tutoriels/unity/13-deplacer-GameObject/
Votre commentaire