Created
November 15, 2013 16:12
-
-
Save david-hodgetts/7486918 to your computer and use it in GitHub Desktop.
destruction d'un gameobject dont le collider rentre en intersection avec un rayon. (cours sae) (unity3d)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Caster : MonoBehaviour { | |
void Update(){ | |
Vector3 rayOrigin = transform.position; // l'origine du rayon: la position du gameobject | |
Vector3 rayDirection = transform.up; // la direction du rayon: l'axe local y positif | |
float rayLength = 10; // la longeur du rayon | |
Debug.DrawLine(rayOrigin, rayOrigin + rayDirection * rayLength); | |
// on vérifie le raycast seuleument sur le click de la souris (boutton gauche) | |
if (Input.GetMouseButton(0)){ | |
RaycastHit hit; // on déclare un objet qui va contenir les informations détaillant | |
// l' intersection du rayon avec un éventuel collider | |
// Physics.Raycast retourne 'true' si une intersection a été détecté | |
// avec un collider. L'objet 'hit' est renseigné dans ce cas | |
if (Physics.Raycast(rayOrigin, rayDirection, out hit, rayLength)){ | |
// on obtient le gameobject auquel appartient le collider qui | |
// est en intersection avec le rayon. Et on le détruit. | |
Destroy (hit.collider.gameObject); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment