- Home /
Help with Raycast on Render Texture
Hey all, I'm a bit new to coding but I have a very specific issue that I haven't seen too much documentation/explanation for. I'm a bit of a beginner, but I'm trying to learn so bear with me. I've managed to find and fix most other issues in my game through trial and error, but this one issue is a bit hard to deal with. I'll lay out all the factors at play:
I'm using two canvas', one for a Render Texture and the other for UI elements. I may only need one, but even putting all of it under one canvas doesn't seem to fix the issue, so I don't think the canvas' themselves are the issue, I think it's the Raycast and Render Texture not working together right.
The first Canvas: I'm using a Render Texture and attaching it to the main camera so that I can achieve a low-res aesthetic, and it's working wonderfully. It basically just takes the main camera, then scales the resolution down, reapplying it to the camera through a texture, becoming the only thing the player sees. I think this is the root of the issue.
The second Canvas: UI elements, like text and other things. I have text in this canvas that appears if certain Raycast requirements are met. Again, I don't think the two canvas' matter, but I'm putting them here for all the cards to be on the table.
The Raycast script is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCasting : MonoBehaviour
{
public static float DistanceFromTarget;
public float ToTarget;
void Update()
{
RaycastHit Hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Hit))
{
ToTarget = Hit.distance;
DistanceFromTarget = ToTarget;
}
}
}
Right now, what I'm trying to accomplish is having the player be able to change scenes by walking up to a door, the Raycast detecting a door trigger, and then UI elements appearing due to that detection. The script below for the door works when I disable the Render Texture canvas, so I know it works fine, but that disables the low-res aesthetic that I'm hoping for. When I activate the Render Texture canvas, no UI elements appear anymore, and even if they didn't, I can also no longer interact with the door. The UI not appearing is one thing, but the entire script not recognizing my door trigger leads me to believe that the Raycast x Render Texture aren't blended together correctly.
The door script is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class CabinDoor : MonoBehaviour
{
public float TheDistance;
public GameObject ActionKey;
public GameObject ActionText;
public AudioSource DoorSound;
void Update()
{
TheDistance = PlayerCasting.DistanceFromTarget;
}
void OnMouseOver ()
{
if (TheDistance <= 1.5)
{
ActionKey.SetActive(true);
ActionText.SetActive(true);
}
if (Input.GetButtonDown("Action"))
{
if (TheDistance <= 1.5)
{
ActionKey.SetActive(false);
ActionText.SetActive(false);
DoorSound.Play();
SceneManager.LoadScene(1);
}
}
if (TheDistance >= 1.5)
{
ActionKey.SetActive(false);
ActionText.SetActive(false);
}
}
void OnMouseExit()
{
ActionKey.SetActive(false);
ActionText.SetActive(false);
}
}
From what I've gathered from my day or two of researching this issue, the problem may be that the Raycast is not aligning with the Render Texture, or that I am in need of a second Raycast to reapply the coordinates of the Raycast onto the texture, or vice versa, I'm not exactly sure. I've seen some documentation for something like this, but am completely lost as to how I would apply that to this script.
Anyone have an idea of what code and where I could add it to make these two systems blend together properly? Thanks!