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.
- 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.
- 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);
}
}