2015年1月4日 星期日

FPS Gun Game (Simple / Basic)

注意: 

1. TextMesh 這個遊戲物件,這邊內用16:9 解析度製作,如果TextrueMesh 位置有誤,請自行調整。

2. 我本人版本Unity 4.6 所製作,在模型animation 如果操作有誤,請遵守4.0 版本 / 3.6版本的做法。

3. 程式部分: [Range] 以設計者設定為主,這邊已用及行拉式選擇,方便後面以客製化為主,方便以後調整即可。

4. 這邊子彈用拖曳效果所製作(Trail Renderder)。

5. 這邊使用Box Collider 的方式做成彈道及彈道射程。

---------------------------------------------------------------------
Clip(彈夾腳本) :

using UnityEngine;
using System.Collections;

public class Clip : MonoBehaviour {

[Range(0, 30)]
public int bullet = 0;

[Range(0, 2f)]
public float offsetZ;

public TextMesh bulletDateTxtMag;

public ReservcClip clipBulletNumner;


void BulletDateTxtMag(int bulletNumber){
bulletDateTxtMag.text = "Bullet: " + bullet + "  Clip: " + clipBulletNumner.clipInBullet;
}

// Update is called once per frame
void Update () {

if(Input.GetMouseButton(0)){
bullet -= 1;
}
if(bullet <= 0){
bullet = 0;
}
BulletDateTxtMag(bullet);
}
}

---------------------------------------------------------------------
MainGun(主槍腳本) :

using UnityEngine;
using System.Collections;

public class MainGun : MonoBehaviour
{
[Range(1, 500)]
public int sizeY;
[Range(1, 30)]
public int centery;
public Animator gunReloadingAnimation;
public Clip clipDate;

public GameObject bullets;
// Update is called once per frame
void Update (){

gunReloadingAnimation.GetComponent<Animator> ();

if(clipDate.bullet > 0){
if(Input.GetMouseButtonDown(0)){
Frie();
}else{
NoFire();
}
}else if(clipDate.bullet == 0){
NoFire();
}
}

void Frie(){

gunReloadingAnimation.SetInteger ("FrieSw", 1);

Instantiate (bullets, transform.position, transform.rotation);

gameObject.GetComponent<BoxCollider> ().size = new Vector3 (0, 0, sizeY);
gameObject.GetComponent<BoxCollider> ().center = new Vector3 (0, 0, centery);

}

void NoFire(){
gunReloadingAnimation.SetInteger ("FrieSw",2);
gameObject.GetComponent<BoxCollider> ().size = new Vector3 (0, 0, 0);
gameObject.GetComponent<BoxCollider> ().center = new Vector3 (0, 0, 0);
}
}

---------------------------------------------------------------------

ReservcClip(備用彈夾):

using UnityEngine;
using System.Collections;

public class ReservcClip : MonoBehaviour {

[Range(0, 90)]
public int clipInBullet;
[Range(1, 30)]
public int totalnumberofbullets;

public Clip bulletNumber;
public Animator gunReloadingAnimation;

// Update is called once per frame
void Update () {

gunReloadingAnimation.GetComponent<Animator> ();

if(clipInBullet >= totalnumberofbullets & bulletNumber.bullet < totalnumberofbullets){
if (Input.GetKeyDown (KeyCode.R)){
Switchingclip ();
}
}else if(clipInBullet < totalnumberofbullets){

if(Input.GetKeyDown(KeyCode.R)){
UpdateClip();
}
}
}

void UpdateClip(){

gunReloadingAnimation.SetInteger ("FrieSw",3);
bulletNumber.bullet = clipInBullet;

if(bulletNumber.bullet == clipInBullet){
clipInBullet = 0;
gunReloadingAnimation.SetInteger ("FrieSw",4);
}

}

void Switchingclip(){

int complement;
complement = totalnumberofbullets - bulletNumber.bullet;
if(clipInBullet > 0){
gunReloadingAnimation.SetInteger ("FrieSw",3);
clipInBullet -= complement;
if(bulletNumber.bullet < totalnumberofbullets){
bulletNumber.bullet += complement;

}
}
}
}

---------------------------------------------------------------------

BulletPosition (子彈腳本):

using UnityEngine;
using System.Collections;

public class BulletPosition : MonoBehaviour {

[Range(50, 500)]
public int speed;
// Update is called once per frame
void Update () {

transform.Translate (Vector3.forward * Time.deltaTime * speed);

Destroy(this.gameObject, 1);
}

----------------------------------------------------------------

結果圖:















沒有留言:

張貼留言