Files
test/Assets/PerfectWorld/Scripts/Debug/TestCmdGoto.cs

58 lines
1.9 KiB
C#

using System;
using EditorAttributes;
using UnityEngine;
using BrewMonster.Network;
namespace BrewMonster.Scripts
{
public class TestCmdGoto : MonoBehaviour
{
[SerializeField] LayerMask _targetLayer = -1; // Layer mask for raycast target
[Header("DEBUG")]
[SerializeField] Vector3 _target; // For debugging: store the last target point
private void Update()
{
if (UnityEngine.Input.GetKey(KeyCode.LeftControl) &&
UnityEngine.Input.GetMouseButtonDown(0)) // Use GetMouseButtonDown to trigger once per click
{
PerformRaycast();
}
}
private void PerformRaycast()
{
Camera mainCam = Camera.main;
if (mainCam == null)
{
Debug.LogWarning("Main camera not found for raycast");
return;
}
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Perform raycast with layer mask
if (Physics.Raycast(ray, out hit, Mathf.Infinity, _targetLayer))
{
// Check if the hit object is on the target layer
int hitLayer = hit.collider.gameObject.layer;
if ((_targetLayer.value & (1 << hitLayer)) != 0)
{
// Send goto command with hit point coordinates
Vector3 hitPoint = hit.point;
_target = hitPoint;
c2s_CmdGoto(hitPoint.x, hitPoint.y, hitPoint.z);
_target = hitPoint; // Store for debugging
}
}
}
public void c2s_CmdGoto(float x, float y, float z)
{
UnityGameSession.c2s_CmdGoto(x, y, z);
// BMLogger.Log($"[MH] Sent goto command to position: ({x}, {y}, {z})");
}
}
}