Okay the goal here is to create a script, which will use a raycast to detect the surfaces I am aiming for.
When the VR controller is pointed at the specified object while the touchpad is being pressed, a laser will point and the object plane will highlight. When the touchpad is released, the user will teleport to specific coordinates per object the script is attached to.
I’m very new to this, the script below is some form of frankenstein scripting I put together, but of course has many errors.
The first error being the named class (LaserPointer) I was already using in another script so I changed it to (LaserHighlight). This caused a whole slew of errors because the script calls for a different class. Where must I make these changes? I did a ctrl+f of ‘LaserPointer’ and replaced them all with the new class name.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaserHighlight : MonoBehaviour {
// 1
public Transform cameraRigTransform;
// 2
public Transform headTransform;
// 3
public LayerMask teleportMask;
// 4
private bool shouldTeleport;
private SteamVR_TrackedObject trackedObj;
private SteamVR_Controller.Device Controller
{
get {return SteamVR_Controller.Input((internal)trackedObj.indexer);}
}
void Awake()
{
trackedObj = GetComponent<SteamVR_TrackedObject>();
}
// 1
public GameObject laserPrefab;
// 2
private GameObject laser;
// 3
private Transform laserTransform;
// 4
private Vector3 hitPoint;
private void ShowLaser(RaycastHit hit)
{
// 1
laser.SetActive(true);
// 2
laserTransform.position = Vector3.Lerp(trackedObj.transform.position, hitPoint, .5f);
// 3
laserTransform.LookAt(hitPoint);
// 4
laserTransform.localScale = new Vector3(laserTransform.localScale.x, laserTransform.localScale.y, hit.distance);
}
public class HighlightScript : MonoBehaviour {
private Color startcolor; // script for highlight color
private void ShowLaser(RaycastHit hit)
{
startcolor = renderer.material.color;
renderer.material.color = Color.red;
}
void OnMouseExit() // possibly incorrect method
{
renderer.material.color = startcolor;
}
// Use this for initialization
void Start () {
// 1
laser = Instantiate(laserPrefab);
// 2
laserTransform = LaserHighlight.transform;
}
// Update is called once per frame
void Update () {
// 1
if (ControllerStyle.GetPress (SteamVR_Controller.ButtonMask.Touchpad)) {
RaycastHit hit;
// 2
if (Physics.Raycast (SteamVR_TrackedObject.transform.position, Transform.forward, out hit, 100, teleportMask)) {
hitPoint = hit.point;
ShowLaser (hit);
}
} else { // 3
LaserHighlight.SetActive (false);
}
if (Controller.GetPressUp (SteamVR_Controller.ButtonMask.Touchpad) && shouldTeleport) {
SteamVR_Teleporter ();
}
}
private void Teleport()
{
// 1
shouldTeleport = false;
// 2
Vector3 difference = cameraRigTransform.position - headTransform.position;
// 3
difference.y = 0;
//4
cameraRigTransform.position = destination.position;
}
}
}