Je propose un de mes scripts destiné à ceux qui veulent créer un FPS.
Le script est disponible en C# et en JavaScript (je vous conseille vivement de prendre celui en C#, étant donné que le JavaScript sur Unity est voué à disparaître (JS n'est plus ^^)
Le script en JavaScript :
Code : Tout sélectionner
var Cam : Camera;
var NormalPos : Vector3;
var AimPos : Vector3;
var Gun : GameObject;
var isReloading : boolean = false;
var reticule : GameObject;
function Start () {
if(Cam == null) {
Cam = Camera.main;
}
}
function Update () {
if(Input.GetButton("Fire2") && isReloading == false){
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, AimPos, Time.deltaTime * 8);
Cam.fieldOfView = Mathf.Lerp(Cam.fieldOfView, 45, Time.deltaTime * 10);
reticule.SetActive(false);
}else{
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, NormalPos, Time.deltaTime * 8);
Cam.fieldOfView = Mathf.Lerp(Cam.fieldOfView, 60, Time.deltaTime * 10);
reticule.SetActive(true);
}
}
Code : Tout sélectionner
using UnityEngine;
using System.Collections;
public class Aiming : MonoBehaviour {
Camera Cam;
Vector3 NormalPos;
Vector3 AimPos;
GameObject Gun;
bool isReloading = false;
GameObject reticule;
void Start (){
if(Cam == null) {
Cam = Camera.main;
}
}
void Update (){
if(Input.GetButton("Fire2") && isReloading == false){
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, AimPos, Time.deltaTime * 8);
Cam.fieldOfView = Mathf.Lerp(Cam.fieldOfView, 45, Time.deltaTime * 10);
reticule.SetActive(false);
}else{
Gun.transform.localPosition = Vector3.Lerp(Gun.transform.localPosition, NormalPos, Time.deltaTime * 8);
Cam.fieldOfView = Mathf.Lerp(Cam.fieldOfView, 60, Time.deltaTime * 10);
reticule.SetActive(true);
}
}
}
J'espère avoir aidé des gens :p