Files
2024-10-29 17:16:17 +01:00

75 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.XR;
//this scripts handels the laser pointer (also the network sync for it)
public class LineRendererActivator : NetworkBehaviour
{
[SerializeField] public LineRenderer lineRenderer;
[SerializeField] public List<InputDevice> mDevice = new List<InputDevice>();
//network variable to perform the laser pointers sync
private NetworkVariable<bool> isRayActive = new NetworkVariable<bool> (writePerm: NetworkVariableWritePermission.Owner);
void Start()
{
}
void Update()
{
//if the script is attached to the owners player object
if (IsOwner)
{
mDevice.Clear();
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.Right | InputDeviceCharacteristics.Controller, mDevice);
// check if any physical controller is connected
if (mDevice.Count > 0)
{
//try to get the input of the right controllers secondary button (B)
InputDevice rightController = mDevice[0];
rightController.TryGetFeatureValue(CommonUsages.secondaryButton, out bool secondaryDown);
if (secondaryDown || Input.GetKey(KeyCode.O))
{
// activate the line renderer while the button is pressed
lineRenderer.enabled = true;
isRayActive.Value = true;
}
else
{
// deactivate the line renderer if the button is released
lineRenderer.enabled = false;
isRayActive.Value = false;
}
}
//for test case with keyboard inputs only
else
{
if (Input.GetKey(KeyCode.O))
{
// activate the line renderer while the button is pressed
lineRenderer.enabled = true;
isRayActive.Value = true;
}
else
{
// deactivate the line renderer if the button is released
lineRenderer.enabled = false;
isRayActive.Value = false;
}
}
}
else
{
//read the activation variable if not owner
lineRenderer.enabled = isRayActive.Value;
}
}
}