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.

99 lines
3.9 KiB

3 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HTC.UnityPlugin.Vive;
//定义一个滑槽,可以让配件在碰到滑槽后吸附到滑槽上面
public class GunSlider : MonoBehaviour {
private Vector3 startPoint;
private Vector3 endPoint;
public Vector3 sliderOffSet;
[Range(0.0f, 1.0f)]
public float offsetPercentage = 0f;
public bool haveItem;
private GameObject item;
private GunItem itemScript_;
private GameObject leftHand;
private GameObject gun;
public Transform startTransForm;
public Transform endTransForm;
// Use this for initialization
void Start () {
haveItem = false;
leftHand = GameObject.FindGameObjectWithTag("leftHand");
gun = GameObject.FindGameObjectWithTag("gun");
startPoint = startTransForm.localPosition;
endPoint = endTransForm.localPosition;
}
// Update is called once per frame
void Update () {
if (haveItem) {
item.transform.localPosition = startPoint + offsetPercentage * (endPoint - startPoint);
//当物品被拖动时,物品会在滑槽之间移动
if (itemScript_.isSlide) {
//float distanceStart = (leftHand.transform.position - (startPoint + sliderOffSet + gun.transform.position)).sqrMagnitude;
//float distanceEnd = (leftHand.transform.position - (endPoint + sliderOffSet + gun.transform.position)).sqrMagnitude;
float distanceStart = (leftHand.transform.position - startTransForm.position).sqrMagnitude;
float distanceEnd = (leftHand.transform.position - endTransForm.position).sqrMagnitude;
float distanceSE = (endPoint - startPoint).sqrMagnitude;
if (distanceStart + distanceSE <= distanceEnd)
{
offsetPercentage = 0f;
}
else if (distanceEnd + distanceSE <= distanceStart)
{
offsetPercentage = 1f;
}
else {
//可以保证x大于0,且随着distance增大而增大
float x = distanceStart + distanceSE - distanceEnd;
//比例系数,使得当distanceStart == distanceEnd+distanceSE时,x*y==1
float y = 0.5f / distanceSE;
offsetPercentage = x * y;
}
}
if (ViveInput.GetPressUp(HandRole.LeftHand,ControllerButton.Trigger)) {
itemScript_.isSlide = false;
}
}
}
//检测到配件进入时,调用安装算法
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "gunItem") {
//该滑槽没有配件的情况下,可以直接安装配件
if (!haveItem)
{
InstallParts(other);
}
//该滑槽有配件的情况下,会把滑槽的配件与手上所持的配件进行更换
else {
itemScript_.itemBtn.GetComponent<GunItemButton>().ReturnItem();
Destroy(item);
//
InstallParts(other);
//
}
}
}
//安装&替换配件使用的方法
private void InstallParts(Collider other_) {
item = other_.gameObject;
item.tag = "Untagged";
itemScript_ = item.GetComponent<GunItem>();
itemScript_.itemBtn.GetComponent<GunItemButton>().InstallItem(startPoint);
//重置装配位置(如果不重置可能会导致配件偏移)
startPoint = startTransForm.localPosition;
endPoint = endTransForm.localPosition;
//装配的配件放置在固定位置上面
startPoint += itemScript_.itemBtn.GetComponent<GunItemButton>().self_.transform.position;
endPoint += itemScript_.itemBtn.GetComponent<GunItemButton>().self_.transform.position;
haveItem = true;
}
}