I need some help debugging a Metroid prime 3 esc scanning system, the problem im having is the scan pop ups are very inconsistent (ie ill look at the item and click to scan and it just wont sometimes unless im at very particular angles)
This code is in my player movement script i use it to calculate what the player is looking at using values from a scannable objects script.
public bool LookingAt(LayerMask mask, GameObject otherGameobject)
{
RaycastHit hit;
transformOffset = transform;
if (Physics.Raycast(transform.position, transformOffset.forward, out hit, Mathf.Infinity, mask))
{
transformOffset.LookAt(camCon.lookLocation);
if (hit.transform.gameObject == otherGameobject)
{
Debug.DrawRay(transform.position, playerCam.transform.TransformDirection(Vector3.forward) * hit.distance, Color.green);
return true;
}
Debug.DrawRay(transform.position, playerCam.transform.TransformDirection(Vector3.forward) * hit.distance, Color.green);
}
Debug.DrawRay(transform.position, playerCam.transform.TransformDirection(Vector3.forward) * hit.distance, Color.green);
return false;
}
This code is the aforementioned scannable objects code, it feeds a layer mask and the object being scanned into the code above.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScannableObject : MonoBehaviour
{
public GameObject player;
private PlayerMovement playMove;
public string scanText;
public GameObject scanDisplay;
public GameObject scanUiText;
public LayerMask ScanLayer;
private RaycastHit hit;
private TMPro.TextMeshProUGUI display;
private ScanVisorLogic scanLogic;
public bool isKeyPadPuzzle = false;
public GameObject keyPad;
private KeyPadPuzzleLogic keypadLogic;
// Start is called before the first frame update
void Start()
{
if(keyPad != null)
{
Debug.Log("assigned puzzle logic");
keypadLogic = keyPad.GetComponent<KeyPadPuzzleLogic>();
}
playMove = player.GetComponent<PlayerMovement>();
display = scanUiText.GetComponent<TMPro.TextMeshProUGUI>();
scanLogic = player.GetComponent<ScanVisorLogic>();
}
private bool didDetectLook = false;
// Update is called once per frame
void Update()
{
if (scanLogic.isScanning && Input.GetKeyDown(KeyCode.Mouse1))
{
didDetectLook = playMove.LookingAt(ScanLayer, this.gameObject);
if (didDetectLook)
{
if (isKeyPadPuzzle)
{
keypadLogic.StartKeypadPuzzle();
Debug.Log("PuzzleInitiated");
Time.timeScale = 0;
}
else
{
scanDisplay.SetActive(true);
display.text = scanText;
Time.timeScale = 0;
}
}
}
if (Input.GetKey(KeyCode.Mouse0) || Input.GetKeyUp(KeyCode.Mouse1))
{
Time.timeScale = 1;
scanDisplay.SetActive(false);
playMove.transformOffset = null;
}
}
}