Untitled
unknown
plain_text
3 days ago
3.9 kB
10
Indexable
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using perfectbloo.GpuRaycast;
public class GunRaycastPlugin : MVRScript
{
private GpuRaycastApi _api;
private LineRenderer _line;
private bool _isApiValid = false;
public override void Init()
{
try
{
// Initialize the API
_api = new GpuRaycastApi(this);
_isApiValid = true;
SuperController.LogMessage("Gun Raycast: API Initialized.");
}
catch (System.Exception e)
{
SuperController.LogMessage("Gun Raycast Error: Could not link to GpuRaycastApi. Ensure the Scene Plugin is loaded! " + e.Message);
_isApiValid = false;
}
// Setup the Shoot Action for the UI
JSONStorableAction shootAction = new JSONStorableAction("Shoot", Shoot);
RegisterAction(shootAction);
// Create the visual beam object
CreateLaserObject();
SuperController.LogMessage("Gun Plugin: Fully Loaded.");
}
private void CreateLaserObject()
{
GameObject lineObj = new GameObject("GunBeam");
lineObj.transform.SetParent(transform, false);
_line = lineObj.AddComponent<LineRenderer>();
// Setup the laser look
_line.material = new Material(Shader.Find("Sprites/Default"));
_line.startColor = Color.yellow;
_line.endColor = new Color(1f, 0.9f, 0f, 0f);
_line.startWidth = 0.015f;
_line.endWidth = 0.005f;
_line.positionCount = 2;
_line.useWorldSpace = true;
_line.enabled = false;
}
public void Shoot()
{
if (!_isApiValid || _api == null)
{
SuperController.LogMessage("Gun Raycast: Cannot shoot - API not initialized.");
return;
}
Vector3 origin = transform.position;
Vector3 direction = transform.forward;
float maxDistance = 100f;
// Default endpoint if nothing is hit
Vector3 endPoint = origin + (direction * maxDistance);
// Define the LayerMask (Layer 29 is Atoms)
int layerMask = 1 << 29;
try
{
GpuRaycastHit hit = new GpuRaycastHit();
// Perform the GPU Raycast
if(_api.Raycast(origin, direction, out hit, maxDistance, layerMask))
{
SuperController.LogMessage("HIT: " + (hit.Atom != null ? hit.Atom.name : "Unknown Object"));
endPoint = hit.Point;
OnPersonHit(hit);
}
else
{
SuperController.LogMessage("MISS: No targets on Layer 29.");
}
}
catch (System.Exception e)
{
// Providing the StackTrace is key to finding out WHERE it crashed inside the API
SuperController.LogMessage("GPU Raycast Exception: " + e.Message);
SuperController.LogMessage("Stack: " + e.StackTrace);
}
// Always show the beam, even on a miss
StopAllCoroutines();
StartCoroutine(FlashBeam(origin, endPoint));
}
IEnumerator FlashBeam(Vector3 start, Vector3 end)
{
_line.SetPosition(0, start);
_line.SetPosition(1, end);
_line.enabled = true;
yield return new WaitForSeconds(0.05f); // Quick flash
_line.enabled = false;
}
void OnPersonHit(GpuRaycastHit hit)
{
// Add custom logic here (e.g., sound effects, triggers, etc.)
if (hit.Atom != null)
{
// You can access the hit transform/atom here
}
}
// Cleanup when plugin is removed
void OnDestroy()
{
if (_line != null) Destroy(_line.gameObject);
}
}Editor is loading...
Leave a Comment