Hello.
I am trying to make it so when I’m stood in a trigger and looking at a button, I can press the button. My debug text is being called from my button script however and I can’t understand why.
CameraLook.cs
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivty * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivty * Time.deltaTime;
if (!playerController.isDead)
{
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
LookingAtButton();
}
}
// Return true when looking at button
public bool LookingAtButton()
{
if (Physics.Raycast(transform.position, transform.forward, out var hit, 10f))
{
var obj = hit.collider.gameObject;
if (obj.tag == "Button")
{
Debug.Log("Looking at button"); // Debug text in question
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
This seems to be logging each frame correctly, here is the problematic script that is also calling the debug text -
Level1_Button.cs
void OnTriggerStay(Collider other)
{
// Trigger detecting collision with player
if (other.name == "Player")
{
intIcon.SetActive(true);
cameraLookText.SetActive(true);
buttonText.SetActive(true);
if (cameraLook.LookingAtButton() == true && Input.GetKeyDown(KeyCode.E)) // This is line 23
{
ButtonPressed();
}
}
}
Thank you!