Aujourd'hui je partage un script destiné à un FPS (ou à n'importe quel type de jeu, c'est comme vous voulez).
Ce script vous permet de ramasser une caisse de munitions et ainsi augmenter votre nombre de munitions (très simple donc). Shoteject étant votre nombre de munitions actuel.
Voici le script en C# :
Code : Tout sélectionner
using UnityEngine;
using System.Collections;
public class AmmoBox : MonoBehaviour {
int ammo = 30;
GameObject eject;
private ShotEject shoteject;
private bool showGUI = false;
void Start (){
shoteject = GameObject.Find("Eject").GetComponent<"ShotEject">();
}
void Update (){
if(showGUI == true){
if(Input.GetKeyDown("e")){
shoteject.reserve += ammo;
Destroy (gameObject);
}
}
}
void OnTriggerEnter ( Collider hit ){
if(hit.gameObject.tag == "Player"){
showGUI = true;
}
}
void OnTriggerExit ( Collider hit ){
if(hit.gameObject.tag == "Player"){
showGUI = false;
}
}
void OnGUI (){
if(showGUI == true){
GUI.Box( new Rect(Screen.width/2-100,Screen.height/2-12.5f,200,25), "Press E to pickup ammo");
}
}
}
Code : Tout sélectionner
#pragma strict
var ammo : int = 30;
var eject : GameObject;
private var shoteject : ShotEject;
private var showGUI : boolean = false;
function Start () {
shoteject = GameObject.Find("Eject").GetComponent("ShotEject");
}
function Update () {
if(showGUI == true){
if(Input.GetKeyDown("e")){
shoteject.reserve += ammo;
Destroy (gameObject);
}
}
}
function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "Player"){
showGUI = true;
}
}
function OnTriggerExit(hit : Collider){
if(hit.gameObject.tag == "Player"){
showGUI = false;
}
}
function OnGUI(){
if(showGUI == true){
GUI.Box(Rect(Screen.width/2-100,Screen.height/2-12.5,200,25), "Press E to pickup ammo");
}
}