VRTK_ControllerEvents returning null - unity3d

I am trying to make a gun in Unity with SteamVR and VRTK but I can't figure out how to properly get controller input. When I use the SteamVR Tracked Controller script I get an IsMatrixValid error and it messes up my HMD. I am currently trying to use VRTK controller events method but it keeps returning as a null even though I followed the video correctly.
Screenshot of script location in project
Video in question: https://www.youtube.com/watch?v=escwjnHFce0
(14:17)
Script in question:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EZEffects;
using VRTK;
public class GunBehavior : MonoBehaviour {
public VRTK_ControllerEvents controllerEvents;
private SteamVR_TrackedObject trackedObj;
public EffectTracer TracerEffect;
public Transform muzzleTransform;
// Use this for initialization
void Start () {
Debug.Log(controllerEvents);
if (controllerEvents)
{
Debug.Log("Hue22");
}
else
{
Debug.Log("work");
}
}
// Update is called once per frame
void Update () {
if (controllerEvents.triggerPressed)
{
ShootWeapon();
}
}
void TriggerPressed(object sender, VRTK.ControllerInteractionEventArgs e)
{
ShootWeapon();
}
void ShootWeapon()
{
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(muzzleTransform.position, muzzleTransform.forward);
TracerEffect.ShowTracerEffect(muzzleTransform.position, muzzleTransform.forward, 250f);
if (Physics.Raycast(ray, out hit, 5000f))
{
Debug.Log("Hueheuhue");
}
}
}

Delete these 3 lines of code from your scrip and it should work
void TriggerPressed(object sender, VRTK.ControllerInteractionEventArgs e)
{
ShootWeapon();
}

Related

How to load next scene when pressed a button while playing in unity

This what I tried,
I was watching Brackeys
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
};
Your script probably doesn't work if you have 1 scene, or the current scene is at the end of the list in Build Settings.
Also you can use it this way:
//Add scenes in inspector
[SerializeField] private List<Scene> _sceneList;
public void LoadNextScene()
{
int currentScene = SceneManager.GetActiveScene().buildIndex;
if (currentScene < _sceneList.Count)
SceneManager.LoadScene(_sceneList[currentScene + 1].buildIndex);
else
print("Its last scene");
}
You can use SceneManager.LoadScene() method to load the Scene by its name or index in Build Settings.
The SceneManager.GetActiveScene().buildIndex gives you the index number of the current scene and you can add an incremental value to navigate to the next scene.
To do that,
Create a new script named SceneController and methods as follows,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void LoadMenuScene() {
SceneManager.LoadScene("MenuScene");
}
public void NextScene() {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void ReloadScene() {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Add the script to the Canvas object
Add the method to the button OnClick event in the inspector
NB: You can also use name or index values to load the scene (like LoadMenuScene)
For more: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

unity ads not showing up

i followed the official unity tutorial on how to put bannner ads on my game, i set everything up in the unity dashboard, and in the editor i see a rectangle that says the ad should be there, but when i build the game nothing shows up, here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class bannerad : MonoBehaviour
{
bool testMode = false;
public string gameId = "******" (my game id is here i just dot want to share it);
// Start is called before the first frame update
IEnumerator Start()
{
Advertisement.Initialize(gameId, testMode);
while (!Advertisement.IsReady("Lost_Ad"))
{
yield return null;
}
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Show("Lost_Ad");
}
}
anyone knows what is the solution?
Are you ever calling this function? If you just replaced the Start function with this IEnumerator it will not get called unless you call it.
private void Start()
{
Advertisement.Initialize(gameId, testMode);
StartCoroutine(StartAds());
}
private IEnumerator StartAds()
{
// your code here
}

How to network a VR button with PUN in Unity?

I've been working on a solution for weeks now and I think it's time to reach out. I'm trying to make a button that plays a sound when pressed by a controller, everyone will hear that sound. Using VRTK and PlayoVR, I'm able to make a non-networked version where the player can put their hand through a cube, click the trigger from the controller, and it makes a sound.
This is the code for that cube:
namespace VRTK.Examples {
using UnityEngine;
public class Whirlygig : VRTK_InteractableObject
{
public GameObject AudioSource;
public AudioSource LeftSpeaker;
public override void StartUsing(VRTK_InteractUse currentUsingObject =
null)
{
AudioSource.GetComponent<AudioSource>().Play();
}
}
}
Where I get lost is how to network it with Photon Unity Networking. This is what I have:
namespace PlayoVR
{
using UnityEngine;
using VRTK;
using UnityEngine.Video;
using NetBase;
public class PlaySync : Photon.MonoBehaviour
{
public AudioSource LeftSpeaker;
public GameObject Whirlgig;
private bool StartUsing;
// Use this for initialization
void Awake()
{
GetComponent<VRTK_InteractableObject>().InteractableObjectUsed +=
new InteractableObjectEventHandler(DoPlay);
}
void DoPlay(object sender, InteractableObjectEventArgs e)
{
StartUsing = true;
}
// Update is called once per frame
void Update()
{
// Handle firing
if (StartUsing)
{
CmdPlay();
StartUsing = false;
}
}
void CmdPlay()
{
photonView.RPC("NetPlay", PhotonTargets.All);
}
[PunRPC]
void NetPlay()
{
LeftSpeaker.Play();
}
}
}
As you can probably see, I'm a beginner. With this code, when I put my hand in the cube and press the trigger, nothing happens. If anyone can provide any help or even an alternative, I'd be very grateful.
Kind regards,
TheMusiken

Unity GestureRecognizer ManipulationStarted and -updated are not working

Please look at the code below, if I use this structure for Tap, Hold and Navigation in Unity for Hololens, it works as expected. But Manipulation does not even get called. I looked at Holokit but I could not understand how holoKit is firing them. I also do not want to import this heavy Holokit on my simple, light project. I am really thankful, anybody can tell me how I can use a simple Gesture Recognizer Manipulation delegate.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.WSA.Input;
namespace Assets.Scripts
{
public class GestureRecognizerManager : MonoBehaviour
{
private GestureRecognizer _recognizer;
private void Awake()
{
_recognizer = new GestureRecognizer();
_recognizer.ManipulationStarted += RecognizerOnManipulationStarted;
_recognizer.ManipulationUpdated += RecognizerOnManipulationUpdated;
_recognizer.StartCapturingGestures();
}
private void RecognizerOnManipulationUpdated(ManipulationUpdatedEventArgs obj)
{
Debug.Log("D");
}
private void RecognizerOnManipulationStarted(ManipulationStartedEventArgs obj)
{
Debug.Log("K");
}
private void OnApplicationQuit()
{
_recognizer.ManipulationStarted -= RecognizerOnManipulationStarted;
_recognizer.ManipulationUpdated -= RecognizerOnManipulationUpdated;
_recognizer.StopCapturingGestures();
_recognizer.Dispose();
}
}
}
Ok, it works by this line of code in initialization stage:
_recognizer.SetRecognizableGestures(GestureSettings.ManipulationTranslate);

In VR development ,how can I make a handle shotting?

I want to make an example of shot,
then I wrote this in the handle button event,
using UnityEngine;
using System.Collections;
public class fire : MonoBehaviour {
public GameObject bullet;
SteamVR_TrackedObject trackedObj;
void start() {
trackedObj = GetComponent<SteamVR_TrakedObject>();
}
void Update() {
var device = SteamVR_Controller.Input((int)trackedObj.index);
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)) {
GameObejct obj = Instantiate(bullet,transform.position);
Vector3d fwd = transform.TransformDirection(Vector3.forward);
obj.GetComponent.<Rigidbody>().AddForce(fwd*2800);
}
}
}
but when debugging and I press handle button ,it didn't produce a bullet,and had erred at the line
var device = SteamVR_Controller.Input((int)trackedObj.index);,
the error is:
Object reference not set to an instance of an object.
First You should need to confirm that your fire script is attached to your controller object and your controller object also attached SteamVR_TrackedObject script (which provide by steam plugin).
Then, lastly ensure this line is executing
void start() {
trackedObj = GetComponent<SteamVR_TrakedObject>();
}