Header
Unity 2D [E1] - Detecting Collisions
in: CSharp  So/Code: Generico Data: 23/02/2016
Ora: 12.16:46
Articolo visualizzato: 2121 volte

This is a mini tutorial in Unity 2D environment with the aim of simply explaining the management of collisions . There will be different , where we will explain the most common problems encountered in the development of 2D games .

The collision management is the basis of any video game , in Unity you can handle them in a very fast and intuitive . Suppose you have an object named " Box " with a sprite depicting a box .

In this item we applied , as well as the sprite , even a 2D Box Collider and a script that we will create . The inclusion of the BoxCollider2D makes possible the interception of collisions as well as the definition of the collision space via the "Edit Collider " .

We see in detail how to edit the script to handle the interception .

We have basically three methods : OnCollisionEnter2D , OnCollisionExit2D , OnCollisionStay who will receive as a parameter the Collision . This contains information about the collision respectively incoming , outgoing and in the act . The information is , for example, collision points , the speed etc ...

 

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class BoxColliderScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

	}

	void OnCollisionEnter2D (Collision collisionInfo)
	{
		// points of collision
		foreach (ContactPoint contact in collisionInfo.contacts) {
			Debug.DrawRay(contact.point, contact.normal, Color.white);
		}

		// velocity
		//collisionInfo.relativeVelocity
	}

	void OnCollisionExit2D (Collision collisionInfo)
	{
		// exit collision object
		print("No longer in contact with " + collisionInfo.transform.name);
	}

	void OnCollisionStay(Collision collisionInfo) {

		// points of collision
		foreach (ContactPoint contact in collisionInfo.contacts) {
			Debug.DrawRay(contact.point, contact.normal, Color.white);
		}

	}

}

Commenti Commenti (0) | User Autore: Guido Camerlingo (Guiz)
Tags: unity 2D videogame gestione collisioni collider 2d detect collision





Articoli Correlati
Unity 2D [E1] - Detecting Collisions
Unity 2D [E1] - Gestire le collisioni
Ubuntu 10.10 Maverick Meerkat RC (Release Candidate)
Ubuntu 12.04 Precise Pangolin ecco la beta
Unity 2D [ E2 ] - Apply the effect vibration / earthquake to a sprite

Commenti

Scrivi Commento
Codice Verifica

Commento massimo 5000 caratteri.(Tutti i campi contrassegnati da * sono obbligatori).

   

Ricerca

Glossario Naviga nel nostro glossario!
Scopri il gergo dei Geek!


 ULTIMI ARTICOLI


 Giochi in uscita oggi

Nessuna uscita prevista


 [EN] Last Articles

 Siti Amici

Visita il Blog Roll
Contattaci! Diventa nostro amico!



 Amazon


 Hot Downloads

3706

 Categorie

83
163
57
87
13
2
20
36
1
2
5
5
128
7
24
15
58
45

 Code

11
11
3
5
2
23
1
7

 Games

19
45
13

 GUIDE


 Novità Downloads

61
42
3706

 Tags Cloud

novità flash 5g Leggera ottiche 66% sinistra apple fuoco cresce fuoco olimpus mac fuoco mobile olimpus antenna del 18% di



 CopyRight

Valid XHTML 1.0 Transitional CSS Valido! [Valid RSS] Creative Commons License


Geek-Blog by Flavio Mandato, Giuseppe Vaccaro, Guido Camerlingo, Stefano Natale, Domenico Cavallo is licensed under a Creative Commons Attribuzione-Non opere derivate 2.5 Italia License.
Based on a work at www.geek-blog.it.
Permissions beyond the scope of this license may be available at http://www.geek-blog.it/

Disclaimer - Responsabilità - Pagina generata in 0.137 secondi. Geek-Blog.it