一击蹲、游戲場景
二署拟、創(chuàng)建怪物
CreateMonster
using UnityEngine;
using System.Collections;
public class CreateMonster : MonoBehaviour {
//產(chǎn)生的怪物預制體
public GameObject Monster;
//目標位置
public Transform TargetPositon;
//創(chuàng)建時播放的聲音文件
public AudioClip CreateClip;
AudioSource AudioSource;
//最長刷怪時長
float MaxColdDownTime = 10;
//最短帥怪時長
float MinColdDownTime = 4;
float CurrentColdDownTime;
void Start()
{
//初始化下次刷怪事件
CurrentColdDownTime = MinColdDownTime;
//獲取AudioSource屬性
AudioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
//通過Time.deltaTime來獲取每一幀執(zhí)行的時間
CurrentColdDownTime -= Time.deltaTime;
//當冷卻時間完成后創(chuàng)建怪物并減少刷怪冷卻時間
if (CurrentColdDownTime <= 0)
{
InstantiateMonster();
CurrentColdDownTime = MaxColdDownTime;
if (MaxColdDownTime > MinColdDownTime)
{
MaxColdDownTime -= 0.5f;
}
}
}
void InstantiateMonster()
{
//播放怪物吼叫聲音
AudioSource.PlayOneShot(CreateClip);
//根據(jù)預制體創(chuàng)建怪物
GameObject go = Instantiate(Monster);
//將創(chuàng)建出來的怪物放置到自己下面,方便管理
go.transform.parent = this.transform;
//通過Random類隨機放置怪物在傳送門口
go.transform.position = this.transform.position + new Vector3(Random.Range(-5, 5), 0, Random.Range(-2.5f, 2.5f));
go.GetComponent<EnermyController>().TargetTransform = TargetPositon;
}
}
三歌豺、給怪物添加自動尋路
四芯丧、給怪物添加動畫
五、怪物行為控制
using UnityEngine;
using System.Collections;
public class EnermyController : MonoBehaviour {
//目標位置
public Transform TargetTransform;
//總血量
int HP = 2;
//導航組件
UnityEngine.AI.NavMeshAgent NavMeshAgent;
//動畫組件
Animator Animator;
// Use this for initialization
void Start () {
//初始化變量
NavMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
NavMeshAgent.SetDestination(TargetTransform.transform.position);
Animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
//死亡直接返回
if (HP <= 0)
{
return;
}
//獲取當前動畫信息
AnimatorStateInfo stateInfo = Animator.GetCurrentAnimatorStateInfo(0);
//判斷當前的動畫狀態(tài)是什么世曾,并進行對應(yīng)的處理
if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.Run") && !Animator.IsInTransition(0))
{
Animator.SetBool("Run", false);
//玩家有移動重新檢測缨恒,目前不會移動
if (Vector3.Distance(TargetTransform.transform.position, NavMeshAgent.destination) > 1)
{
NavMeshAgent.SetDestination(TargetTransform.transform.position);
}
//進入攻擊距離跳轉(zhuǎn)到攻擊動畫,否則繼續(xù)跑動
if (NavMeshAgent.remainingDistance < 3)
{
Animator.SetBool("Attack",true);
}
else
{
Animator.SetBool("Run", true);
}
}
if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.Attack") && !Animator.IsInTransition(0))
{
Animator.SetBool("Attack", false);
//玩家有移動重新檢測轮听,目前不會移動
if (Vector3.Distance(TargetTransform.transform.position, NavMeshAgent.destination) > 1)
{
NavMeshAgent.SetDestination(TargetTransform.transform.position);
}
//進入攻擊距離跳轉(zhuǎn)到攻擊動畫骗露,否則繼續(xù)跑動
if (NavMeshAgent.remainingDistance < 3)
{
Animator.SetBool("Attack", true);
NavMeshAgent.Stop();
}
else
{
Animator.SetBool("Run", true);
}
//if (stateInfo.normalizedTime >= 1.0f)
//{
// Manager.Instance.UnderAttack();
//}
}
if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.Damage") && !Animator.IsInTransition(0))
{
Animator.SetBool("Attack", false);
Animator.SetBool("Run", false);
//玩家有移動重新檢測,目前不會移動
if (Vector3.Distance(TargetTransform.transform.position, NavMeshAgent.destination) > 1)
{
NavMeshAgent.SetDestination(TargetTransform.transform.position);
}
//進入攻擊距離跳轉(zhuǎn)到攻擊動畫血巍,否則繼續(xù)跑動
if (NavMeshAgent.remainingDistance < 3)
{
Animator.SetBool("Attack", true);
NavMeshAgent.Stop();
}
else
{
Animator.SetBool("Run", true);
}
}
}
//當被槍擊中時調(diào)用
public void UnderAttack()
{
//扣血并判斷是否死亡萧锉,如果死亡則直接跳到死亡動畫,否則跳到受傷動畫
HP--;
if (HP <= 0)
{
Animator.Play("Death");
Destroy(GetComponent<Collider>());
Destroy(GetComponent<UnityEngine.AI.NavMeshAgent>());
}
else
{
Animator.Play("Damage");
}
}
//怪物攻擊由動畫事件調(diào)用
public void Attack()
{
Manager.Instance.UnderAttack();
}
}
六述寡、游戲管理類
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Manager : MonoBehaviour {
//Manager單例
public static Manager Instance;
//當前玩家HP
public int CurrentHP = 10;
//添加傳送門和重玩引用
public GameObject Portals;
public GameObject Replay;
// Use this for initialization
void Awake () {
//實現(xiàn)單例
if (Instance == null)
{
Instance = this;
}
else
{
Debug.LogError("僅有一個Manager");
}
}
//收到怪物攻擊
public void UnderAttack () {
CurrentHP--;
//如果血量小于0游戲失敗
if(CurrentHP < 0)
{
EndGame();
}
}
void EndGame()
{
Destroy(Portals);
Replay.SetActive(true);
}
//重新開始游戲
public void ReStartGame()
{
SceneManager.LoadScene("FPS");
//Application.LoadLevel("FPS");
}
}
七柿隙、怪物攻擊
八、主角攻擊
1鲫凶、添加手槍模型
2禀崖、剩余子彈
3、添加開槍動畫
4螟炫、實現(xiàn)開槍邏輯
using UnityEngine;
using System.Collections;
public class GunShoot : MonoBehaviour {
//添加引用
public Animator Animator;
public Transform GunPoint;
public GameObject Spark;
public TextMesh textMesh;
public AudioClip Fire;
public AudioClip Reload;
AudioSource AudioSource;
bool isReloading = false;
int maxBullet = 12;
int currnetBullet;
SteamVR_TrackedController SteamVR_TrackedController;
// Use this for initialization
void Start () {
//創(chuàng)建監(jiān)聽
SteamVR_TrackedController = GetComponent<SteamVR_TrackedController>();
SteamVR_TrackedController.TriggerClicked += TriggerClicked;
SteamVR_TrackedController.Gripped += Gripped;
currnetBullet = maxBullet;
AudioSource = GetComponent<AudioSource>();
}
//扳機扣下時的開槍邏輯
void TriggerClicked(object sender, ClickedEventArgs e)
{
//如果在換彈中直接返回
if (isReloading)
{
return;
}
//如果沒有子彈也直接返回
if (currnetBullet > 0)
{
currnetBullet --;
textMesh.text = currnetBullet.ToString();
}
else
{
return;
}
//播放開槍聲音
AudioSource.PlayOneShot(Fire);
Animator.Play("PistolAnimation");
Debug.DrawRay(GunPoint.position, GunPoint.up*100, Color.red, 0.02f);
Ray raycast = new Ray(GunPoint.position, GunPoint.up);
RaycastHit hit;
//根據(jù)Layer來判斷是否有物體擊中
LayerMask layer = 1<<( LayerMask.NameToLayer("Enermy"));
bool bHit = Physics.Raycast(raycast, out hit,10000,layer.value);
//擊中扣血邏輯
if (bHit)
{
Debug.Log(hit.collider.gameObject);
EnermyController ec = hit.collider.gameObject.GetComponent<EnermyController>();
if (ec != null)
{
ec.UnderAttack();
GameObject go = GameObject.Instantiate(Spark);
go.transform.position = hit.point;
Destroy(go, 3);
}
else
{
Manager.Instance.ReStartGame();
}
}
}
//手柄握下的邏輯
void Gripped(object sender, ClickedEventArgs e)
{
if (isReloading)
{
return;
}
isReloading = true;
Invoke("ReloadFinished", 2);
AudioSource.PlayOneShot(Reload);
}
//換彈結(jié)束
void ReloadFinished()
{
isReloading = false;
currnetBullet = maxBullet;
textMesh.text = currnetBullet.ToString();
}
}
5波附、設(shè)置怪物層級
九、游戲重新開始
Paste_Image.png