- Home /
Weapon Switch System for Gear VR Controller
Hi, I'm making a game for the Gear VR that involves a weapon switch system. The two weapons are a sword, and a gun. The sword basically uses the rotation of the controller like a Wii remote if you've ever used one, so the player can swipe, block, etc. The gun is a simple point and shoot.
I'm using a weapon switch system that I've used many times before in normal games, disabling one weapon, enabling another. However with the Gear VR controller, I have to check which hand the player is using (right or left) then disable both weapons in the hand they're not using. This is done using a script on the controller prefab provided by Oculus, I've made some changes: changed the m_model to an array which includes the weapons.
/// <summary>
/// The root GameObject that should be conditionally enabled depending on controller connection status.
/// </summary>
public GameObject[] m_model;
/// <summary>
/// The controller that determines whether or not to enable rendering of the controller model.
/// </summary>
public OVRInput.Controller m_controller;
private bool m_prevControllerConnected = false;
private bool m_prevControllerConnectedCached = false;
void Update()
{
bool controllerConnected = OVRInput.IsControllerConnected(m_controller);
if ((controllerConnected != m_prevControllerConnected) || !m_prevControllerConnectedCached)
{
m_model[0].SetActive(controllerConnected);
m_model[1].SetActive(controllerConnected);
m_prevControllerConnected = controllerConnected;
m_prevControllerConnectedCached = true;
}
if (!controllerConnected)
{
return;
}
}
Here's my weapon switch script:
GameObject weaponSword;
GameObject weaponGun;
void Update()
{
if(weaponSword == null || weaponGun == null)
{
assignWeapons();
}
if(OVRInput.GetDown(OVRInput.Button.DpadUp))
{
weaponGun.SetActive(true);
weaponSword.SetActive(false);
}
if(OVRInput.GetDown(OVRInput.Button.DpadDown))
{
weaponSword.SetActive(true);
weaponGun.SetActive(false);
}
}
void assignWeapons()
{
weaponSword = GameObject.FindGameObjectWithTag("techSword");
weaponGun = GameObject.FindGameObjectWithTag("techGun");
Debug.Log("Weapons Assigned " + weaponSword.name + " " + weaponGun.name);
weaponGun.SetActive(false);
}
The problem is that the gun isn't being disabled after it finds the weapons, and it is finding the weapons (the debug tells as much). I would just expose the variables and assign the game objects in the editor, as that seems to work for some reason, but the thing is, I have to find and activate/disable only the weapons in the right or left hand. So first it checks which hand the controller is being used in (right) then it disables the models for the left hand so there isn't a static model sitting there, then my script populates my game object variables using find with tag (and it does find them, the debug works and shows the correct name) but I'm getting my gun overlapping with my sword.
Why do you have to have 2 sets one for each hand? OVRInput's position is going to take into account which hand is active and position that hand accordingly (and I presume the weapon you have attached to the hand). A gun or a sword is symmetrical so they would not have to swap models regardless of the hand it's attached to.
Your answer
Follow this Question
Related Questions
Weapon Switch Script Help 1 Answer
How to Change a Weapon with tag? 1 Answer
Making Weapon Switching Code more effective. 3 Answers
PhotonNetwork Switching Weapons (network sync) 3 Answers
Weapon Switching 3th Person 0 Answers