Missing links in getting a first sort of game going.

Hello!

Thanks in advance to who ever can help me.

I have gotten into Unity and trying to develop content for the HTC VIVE.

I have a scene built and I am trying to sort out a few tricks before I add it to a larger game attempt.

  1. If you have played the Valve Lab demo there is a quadcopter space ship that steers in the direction of the controller. That is my first thing I am trying to figure out. I made a cube turret and gave it a bobble head. I want to have the head follow the same rotation and angle as the controller like the Lab Drone. I have an idea on a shooter that I want to use the controller to aim using a laser pointer and have the turrets aim at what ever the laser pointer is pointing at.

Side note I know I could use the collision of the laser pointer with a Ray cast and have the turret transform look at the position but the catch is when its pointing at nothing (open sky) then I still need it to be following the laser pointer.

  1. Sound! it seems like it should be an easy on event trigger but I have yet to be able to get a simple AudioSource to play on triggerpress. I made a turret I gave it an audio source with an attached wav file. I made a turret control script that fires the turrets and that part works fine. Now when I go and add a few lines to address the sound being played it breaks the turret firing and still does not play the sound. See below the script im using for my turret. I currently have the playsound commented out as the turrets work before that.

Ive literally been through 50 + videos and more than 100 web sites on creating and using VR in Unity but have yet to solve these 2 road blocks. I still have to sort out AI and GUI and scoring and beginning and ending a round but I want to get the mechanics to work in sandbox before I try to make it into a playable game.

using UnityEngine;
using System.Collections;

public class CannonBehavior : MonoBehaviour {

//init the sound
public AudioClip SoundToPlay;
public float Volume;
AudioSource audio;
//init cannon properties
public Transform m_cannonRot;
public Transform m_muzzle;
public GameObject m_shotPrefab;
//init controller
public GameObject controllerRight;
private SteamVR_TrackedObject trackedObj;
private SteamVR_Controller.Device device;
private SteamVR_TrackedController controller;

void Start ()
{
controller = controllerRight.GetComponent<SteamVR_TrackedController>();
controller.TriggerClicked += TriggerPressed;
trackedObj = controllerRight.GetComponent<SteamVR_TrackedObject>();
}

private void TriggerPressed(object sender, ClickedEventArgs e)
{
ShootWeapon();

}
public void PlaySound()
{
audio = GetComponent();
audio.PlayOneShot(SoundToPlay, Volume);
}

public void ShootWeapon()
{

RaycastHit hit = new RaycastHit();
Ray ray = new Ray(m_muzzle.position, m_muzzle.forward);

device = SteamVR_Controller.Input((int)trackedObj.index);
device.TriggerHapticPulse(400);
// PlaySound();
GameObject go = GameObject.Instantiate(m_shotPrefab, m_muzzle.position, m_muzzle.rotation) as GameObject;
GameObject.Destroy(go, 3f);

if(Physics.Raycast(ray, out hit, 5000f))
{
if (hit.collider.attachedRigidbody)
{
Debug.Log(“You hit” + hit.collider.gameObject.name);

} //To be added to shootable objects

}
}

// Update is called once per frame
void Update ()
{

//Figure out how to move turrets with controller.
// m_cannonRot.transform.LookAt();
//m_cannonRot.transform.Rotate(Vector3.up, Time.deltaTime * 100f);

}

}

OK so I made some progress. I can get the turret to follow the controller BUT the turret is 90 degrees forward of the controller. Currently you have to point the controller at the ceiling to have the turret level. Ive tried setting the rotation on the turret X axis to -90 but once it loads its back to facing down.

Still no sound but I did notice that 3 other examples I played with the sound didnt work there either. The onawake sounds play and if i set my sound to on awake the inital does play fine but not the trigger press in my game or any of the examples so far. Is there a setting or something I am missing in unity and vive?

New code for turret is this so far:

using UnityEngine;
using System.Collections;
using UnityEngine.VR;

public class CannonBehavior : MonoBehaviour {

//init cannon properties
public Transform m_muzzle;
public GameObject m_shotPrefab;
//init controller
public GameObject controllerRight;
private SteamVR_TrackedObject trackedObj;
private SteamVR_Controller.Device device;
private SteamVR_TrackedController controller;
//Controller Tracking Init
[SerializeField] private float m_Damping = 0.5f; // The damping with which this gameobject follows the controller.
[SerializeField] private AudioSource m_GunAudio; // The audio source which plays the sound of the gun firing.
private const float k_DampingCoef = -20f; // This is the coefficient used to ensure smooth damping of this gameobject.

void Start ()
{
controller = controllerRight.GetComponent<SteamVR_TrackedController>();
controller.TriggerClicked += TriggerPressed;
trackedObj = controllerRight.GetComponent<SteamVR_TrackedObject>();
}

private void TriggerPressed(object sender, ClickedEventArgs e)
{
ShootWeapon();

}

public void ShootWeapon()
{

RaycastHit hit = new RaycastHit();
Ray ray = new Ray(m_muzzle.position, m_muzzle.forward);

device = SteamVR_Controller.Input((int)trackedObj.index);
device.TriggerHapticPulse(400);
GameObject go = GameObject.Instantiate(m_shotPrefab, m_muzzle.position, m_muzzle.rotation) as GameObject;
GameObject.Destroy(go, 3f);
m_GunAudio.Play();

if(Physics.Raycast(ray, out hit, 5000f))
{
if (hit.collider.attachedRigidbody)
{
Debug.Log(“You hit” + hit.collider.gameObject.name);

} //To be added to shootable objects

}
}

// Update is called once per frame
void Update ()
{
// Smoothly interpolate this gameobject’s rotation towards that of the user/camera.
transform.rotation = Quaternion.Slerp(transform.rotation, InputTracking.GetLocalRotation(VRNode.RightHand),
m_Damping * (1 - Mathf.Exp(k_DampingCoef * Time.deltaTime)));
}

}

More progress,

I fixed the sound. I ended up having to load resource in the script to get it to load because it apparently does not auto load on prefab?

main problem left for now is how to fix the 90 degree offset on the position of the controller vs the turret. I need it to follow the laser ray cast out the forward direction of the controller and right now it follows the bottom or trigger side.