VR模拟枪支打靶,消灭鬼怪,换弹以及上弦等等硬核枪支操作。 使用HTCVive设备,开启SteamVR进行游玩。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.2 KiB

3 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using HTC.UnityPlugin.Vive;
public class PlayerControl : MonoBehaviour {
//左右扳机键
public UnityEvent leftTriggerClick;
public UnityEvent rightTriggerClick;
//左手换武器用
public UnityEvent upPadClick;
public UnityEvent downPadClick;
public UnityEvent leftPadClick;
public UnityEvent rightPadClick;
//右手行走,这里不定义新的右手Pad键了
//左右手指针相关
public GameObject leftPointer;
public GameObject rightPointer;
//是否处于暂停状态
private bool isPause;
private bool isOpenLeftHandWeapon;
//武器切换判定
private float padTouchX_;
private float padTouchY_;
public GameObject LeftHandWeapon_;
public GameObject[] RightHandWeapons_;
public GameObject Weapon_;
public GameObject Needle_;
//教学模式上锁
public bool lockChange;
//治疗针相关
public int needleHpRemain;
//盾牌恢复相关
public int shieldRecover;
public bool endRecoverShield;
// Use this for initialization
void Start () {
upPadClick.AddListener(Padup);
downPadClick.AddListener(PadDown);
leftPadClick.AddListener(PadLeft);
rightPadClick.AddListener(PadRight);
isPause = false;
isOpenLeftHandWeapon = false;
//治疗针冷却
StartCoroutine(addHpAction());
//StartCoroutine(addShieldRecover());
lockChange = false;
}
// Update is called once per frame
void Update () {
//
if (ViveInput.GetPressDown(HandRole.LeftHand,ControllerButton.Trigger)) {
leftTriggerClick.Invoke();
}
if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Trigger))
{
rightTriggerClick.Invoke();
}
//游戏暂停
if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Menu) && !isPause) {
isPause = true;
Time.timeScale = 0.0f;
}
if (ViveInput.GetPressDown(HandRole.LeftHand, ControllerButton.Menu) && isPause)
{
isPause = false;
Time.timeScale = 1.0f;
}
//武器切换
if (ViveInput.GetPressDown(HandRole.LeftHand, ControllerButton.Pad) && !lockChange) {
padTouchX_ = ViveInput.GetPadAxis(HandRole.LeftHand, false).x;
padTouchY_ = ViveInput.GetPadAxis(HandRole.LeftHand, false).y;
if (padTouchY_ > padTouchX_ && padTouchY_> -padTouchX_)
{
upPadClick.Invoke();
}
if (padTouchY_ > padTouchX_ && padTouchY_ < -padTouchX_)
{
leftPadClick.Invoke();
}
if (padTouchY_ < padTouchX_ && padTouchY_ > -padTouchX_)
{
rightPadClick.Invoke();
}
if (padTouchY_ < padTouchX_ && padTouchY_ < -padTouchX_)
{
downPadClick.Invoke();
}
}
if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Grip)) {
GripRight();
}
}
public void ReturnMainMenu() {
SceneManager.LoadScene(0);
}
private void Padup() {
RightHandWeapons_[0].SetActive(true);
RightHandWeapons_[1].SetActive(false);
RightHandWeapons_[2].SetActive(false);
}
private void PadDown() {
if (!isOpenLeftHandWeapon)
{
LeftHandWeapon_.SetActive(true);
isOpenLeftHandWeapon = true;
}
else {
LeftHandWeapon_.SetActive(false);
isOpenLeftHandWeapon = false;
}
}
private void PadLeft() {
RightHandWeapons_[0].SetActive(false);
RightHandWeapons_[1].SetActive(true);
RightHandWeapons_[2].SetActive(false);
}
private void PadRight() {
RightHandWeapons_[0].SetActive(false);
RightHandWeapons_[1].SetActive(false);
RightHandWeapons_[2].SetActive(true);
}
private void GripRight() {
if (Weapon_.activeSelf)
{
Needle_.SetActive(true);
Weapon_.SetActive(false);
}
else {
Weapon_.SetActive(true);
Needle_.SetActive(false);
}
}
public void padUpPublic() {
Padup();
}
public void gripRightPublic() {
GripRight();
}
//缓慢恢复治疗针数值的协程
IEnumerator addHpAction()
{
while (true)
{
if (needleHpRemain < 30)
{
needleHpRemain += 1;
}
yield return new WaitForSeconds(1.0f);
}
}
//缓慢恢复盾牌数值的协程
IEnumerator addShieldRecover() {
while (true)
{
if (shieldRecover < 30)
{
shieldRecover += 1;
}
if (endRecoverShield) {
break;
}
yield return new WaitForSeconds(1.0f);
}
endRecoverShield = false;
}
public void startRecoverShield() {
StartCoroutine(addShieldRecover());
}
public void stopRecoverShield() {
endRecoverShield = true;
}
}