Plan du site  
pixel
pixel

Articles - Étudiants SUPINFO

First Step in Unity 3D

Par Jean-Baptiste AMMIRATI Publié le 19/09/2019 à 18:30:58 Noter cet article:
(0 votes)
Avis favorable du comité de lecture

Introduction

Hello and welcome to this tutorial, today we will learn the basics of Unity by creating a simple cube that we will move and blow up to have a beginning of the game with very simple code. This article will be composed of different parts starting with:

  • The creation of the project

  • Movement / Jump of the cube

  • Create a project.exe

Project implementation

In this first part, we will create a new project. To do this, launch Unity, on the log page create a new 3D project for this example.

Once created, you will have an empty interface, first we will add a "Cube" that we will name "Player". For that we will make a click right where your hierarchy is located, select "3D Object" and then take "Cube" your famous cube will then be added to the existing scene. Do this again manipulation to add a "Plane", in order to have your cube or plane at zero if it wasn't done in the "inspector, then transform", right click again and reset to get the coordinates to zero. Your cube is now halfway into the plane raised its Y-position by 0.5 so that it is perfectly on the plane. Let's move on. now to the script that will allow the cube to move.

Moving / Jumping the cube

To move the cube we will first add a component to our object which will be the rigidbody, it will allow us to manage the gravity of the cube which will allow us to be useful later on as we want to blow it up. In the parameters of the rigidbody select the constraints and check "Freeze Rotation X and Z". This is going to allow to block the rotation of the cube in X and Z.

Then let's create a new script that we'll call CubeMovement and put the following code inside:

Code CubeMovement :

        

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class CubeMovement : MonoBehaviour

{

// On déclare des variables public pour pouvoir

// modifier les valeurs dans l'interface graphique

public float mSpeed = 10.0f;

public float mSpeedRotate = 40.0f;

Vector3 mVelocity;

// Start is called before the first frame update

void Start()

{

}

// Update is called once per frame

void Update()

{

// On initialise le Vecteur

mVelocity = Vector3.zero;

if(Input.GetKey(KeyCode.Z))

{

mVelocity.z = 1.0f;

}

else if(Input.GetKey(KeyCode.S))

{

mVelocity.z = -1.0f;

}

transform.Translate(mVelocity.normalized * Time.deltaTime * mSpeed);

if (Input.GetKey(KeyCode.D))

{

transform.Rotate(0.0f, mSpeedRotate * Time.deltaTime, 0.0f);

}

else if (Input.GetKey(KeyCode.Q))

{

transform.Rotate(0.0f, -mSpeedRotate * Time.deltaTime, 0.0f);

}

}

}

Once the code is in place, drag your file onto the object that will take the different parameters. We'll do the same for the cube jump, this time we will add a tag on the plane we created previously and call it "Ground". This small addition will allow the cube once glued on the plane to be able to jump, if the cube is in the air then the function that will allow you to jump will not have of effect.

Code CubeJump :

        

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class CubeJump : MonoBehaviour

{

public float JumpSpeed = 5.0f;

public bool onGround;

private Rigidbody rb;

// Start is called before the first frame update

void Start()

{

onGround = true;

rb = GetComponent<Rigidbody>();

}

// Update is called once per frame

void Update()

{

if(onGround == true)

{

if(Input.GetButtonDown("Jump"))

{

rb.velocity = new Vector3(0f, JumpSpeed, 0f);

onGround = false;

}

}

}

public void OnCollisionEnter(Collision collision)

{

if (collision.gameObject.CompareTag("Ground"))

{

onGround = true;

}

else

{

onGround = false;

}

}

}

Creating the .exe

To create the.exe it is very simple, first go to "File then Build Settings" once the menu is open, simply add your scene to the interface "Scenes in Build". Then selected your platform you will have a panel of choice, to finally click on "Build and Run" select where you want to have the.exe and it will be will do on its own.

Conclusion

And here is a little tutorial finished, you have learned in a simple and quick way how to move and blow up an object or something. I hope it would have liked you and that you will continue to expand your knowledge in this area.

The different sources used for this article:

https://www.youtube.com/

A propos de SUPINFO | Contacts & adresses | Enseigner à SUPINFO | Presse | Conditions d'utilisation & Copyright | Respect de la vie privée | Investir
Logo de la société Cisco, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société IBM, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Sun-Oracle, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Apple, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Sybase, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Novell, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Intel, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Accenture, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société SAP, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Prometric, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Toeic, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo du IT Academy Program par Microsoft, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management

SUPINFO International University
Ecole d'Informatique - IT School
École Supérieure d'Informatique de Paris, leader en France
La Grande Ecole de l'informatique, du numérique et du management
Fondée en 1965, reconnue par l'État. Titre Bac+5 certifié au niveau I.
SUPINFO International University is globally operated by EDUCINVEST Belgium - Avenue Louise, 534 - 1050 Brussels