你好,我目前正在一项学校项目中,要弄碎物体并握住它们。一直在使用Blender断裂工具,将我的objs导入并用带有石斧的steamVR播放器将其破坏。 obj的破解有点太简单了,一直在寻求改进。一段时间后,我发现了一个叫VR with Andrew的人,他们制作诸如Unity + Vive Tutorial - Melee Damage之类的教程。反复进行近战攻击,将使用墙壁,木头等。
我已经能够在一定程度上修复代码,但仍会卡在某些部分上。他最近在SteamVR上播放的视频有助于加快该过程以及SteamVR的pdf文档的速度,这是他最近在Steamvr 2.2上播放的视频: [Unity] SteamVR 2.2 Input Update
到目前为止,我一直在努力进行更新的脚本使它们能够正常工作,但是我一直陷在10个错误中,无法找到解决问题的方法,感谢您的帮助!
ViveInput脚本:https://pastebin.com/Smm1vhTH
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
namespace Valve.VR.InteractionSystem.Sample
{
public class ViveInput : MonoBehaviour
{
public SteamVR_Action_Boolean mDevice;
// private SteamVR_TrackedObject mTrackeObject = null;
private Interaction mInteraction = null;
public Hand hand;
public GameObject prefabToMagic;
private void OnEnable()
{
if (hand == null)
hand = this.GetComponent<Hand>();
}
void Awake()
{
// mTrackeObject = GetComponent<SteamVR_TrackedObject>();
// mInteraction = GetComponent<Interaction>();
//mDevice = SteamVR_Actions._default.GrabPinch;
}
/*private void Update()
{
if (SteamVR_Input.GetState("resource", "Resource", hand.handType))
//(SteamVR_Actions.farming.Plant[hand.handType].state)
Resource();
} */
private void Update()
{
// mDevice = SteamVR_Input.inActions.GrabPinch.GetStateUp(hand.handType);
#region Trigger
// Down
if (SteamVR_Input.GetState("resource", "Resource", hand.handType))
{
mInteraction.Pickup(mDevice);
}
// Up
if (mDevice.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
mInteraction.Drop(mDevice);
}
// Value
Vector2 triggerValue = mDevice.GetAxis(EVRButtonId.k_EButton_SteamVR_Trigger);
#endregion
#region Grip
/*// Down
if (mDevice.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
{
print("Grip down");
}
// Up
if (mDevice.GetPressUp(SteamVR_Controller.ButtonMask.Grip))
{
print("Grip up");
}
#endregion
#region Touchpad
// Down
if (mDevice.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
{
print("Touchpad down");
}
// Up
if (mDevice.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad))
{
print("Touchpad up");
} */
Vector2 touchValue = mDevice.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
#endregion
}
}
}
交互脚本:https://pastebin.com/Vq72jyCh
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
namespace Valve.VR.InteractionSystem.Sample
{
public class Interaction : MonoBehaviour
{
public GameObject controllerMeshObject;
private FixedJoint mAttachJoint = null;
private Rigidbody mCurrentRigidBody = null;
private List<Rigidbody> mContactRigidBodies = new List<Rigidbody>();
void Awake()
{
mAttachJoint = GetComponent<FixedJoint>();
}
void OnTriggerEnter(Collider collider)
{
if (!collider.gameObject.CompareTag("Interactable"))
return;
// Add the object to contact list
mContactRigidBodies.Add(collider.gameObject.GetComponent<Rigidbody>());
}
void OnTriggerExit(Collider collider)
{
if (!collider.gameObject.CompareTag("Interactable"))
return;
// Remove the object to contact list
mContactRigidBodies.Remove(collider.gameObject.GetComponent<Rigidbody>());
}
public void Pickup()
{
// Get nearest
mCurrentRigidBody = GetNearestRigidbody();
if (!mCurrentRigidBody)
return;
// Set to position of axe
mCurrentRigidBody.transform.position = transform.position;
mCurrentRigidBody.transform.localEulerAngles = new Vector3(0, -180, -90);
// Pass info to axe
mCurrentRigidBody.gameObject.GetComponentInChildren<StoneAxe>().Setup(GetComponent<ViveInput>().mDevice, this);
// Ensure the correct physics settings
mCurrentRigidBody.useGravity = true;
mCurrentRigidBody.isKinematic = false;
// Connect to controller
mAttachJoint.connectedBody = mCurrentRigidBody;
// Disable tomato
controllerMeshObject.SetActive(false);
}
public void Drop(SteamVR_Action_Boolean mDevice)
{
if (!mCurrentRigidBody)
return;
// Apply velocity
if (device != null)
{
mCurrentRigidBody.velocity = device.velocity;
mCurrentRigidBody.angularVelocity = device.angularVelocity;
}
// Disable physics
else
{
mCurrentRigidBody.useGravity = false;
mCurrentRigidBody.isKinematic = true;
}
// Disconnect from controller
mAttachJoint.connectedBody = null;
mCurrentRigidBody = null;
// Enable tomato
controllerMeshObject.SetActive(true);
}
private Rigidbody GetNearestRigidbody()
{
Rigidbody nearestRigidBody = null;
float minDistance = float.MaxValue;
float distance = 0.0f;
// For each object we are touching
foreach (Rigidbody contactBody in mContactRigidBodies)
{
// The length of the vector is square root of (x*x + y*y + z*z).
distance = (contactBody.gameObject.transform.position - transform.position).sqrMagnitude;
if (distance < minDistance)
{
minDistance = distance;
nearestRigidBody = contactBody;
}
}
return nearestRigidBody;
}
}
}
ShatterObject:https://pastebin.com/m5TdCaBr
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;
using Valve.VR;
namespace Valve.VR.InteractionSystem.Sample
{
public class ShatterOnCollision : MonoBehaviour
{
public GameObject mLogOne;
public GameObject mLogTwo;
public ParticleSystem Splinter;
private Collider mCollider = null;
void Awake()
{
mCollider = GetComponent<Collider>();
}
void OnTriggerEnter(Collider collider)
{
// Is it a blade
if (!collider.gameObject.CompareTag("StoneAxe"))
return;
StoneAxe stoneaxe = collider.gameObject.GetComponent<StoneAxe>();
// Is the blade in use?
if (device.mDevice == null)
return;
// Check for log split
Split(stoneaxe);
}
private void Split(StoneAxe stoneaxe)
{
Splinter.Play();
// Split log
if (stoneaxe.mDevice.velocity.magnitude > 3.0f)
{
// Disable collision, so we only split once
mCollider.enabled = false;
// Enable physics for both sides
EnablePhysics(mLogOne);
EnablePhysics(mLogTwo);
}
// Stick Axe
else if (stoneaxe.mDevice.velocity.magnitude > 1.0f)
{
stoneaxe.mInteraction.Drop(null);
}
}
private void EnablePhysics(GameObject log)
{
mLogOne.transform.parent = null;
Rigidbody rigidBody = log.GetComponent<Rigidbody>();
rigidBody.useGravity = true;
rigidBody.isKinematic = false;
}
}
/* void OnCollisionEnter()
{
GameObject.Instantiate(replacement, transform.position, transform.rotation);
Destroy(gameObject);
} */
}
在我的摘录中显示了显示我的Unity Errors的错误。 希望代码和图像有所帮助,如果您有任何建议或技巧可以改善代码的布局,性能等,我想尽可能多地提出批评,通过学习对我的课程有很大帮助从两侧等等!
谢谢,祝你有美好的一天!