How to stop rendertexture from interfering with Raycasting
Hi, I'm working on a project that uses both rendertextures and raycasting, I have a key system that uses raycasting, and a script that pixelates the screen to make it look low res and retro like.
However, it seems that the pixelation script is interfering with my raycasting, as whenever I use the pixelation script it doesnt raycast properly in runtime. I'm not too sure what to do about the issue as there is no compile error or anything to point me in the right direction. Any help on this issue would be greatly appreciated!
GlobalVars.cs (To allow the haveKey bool to be interacted with by both scripts)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public static class GlobalVars{
public static bool haveKey = false;
}
door.cs (Door script) using UnityEngine; using UnityEngine.UI; using System.Collections;
public class door : MonoBehaviour {
void Start()
{
Update();
}
void Update()
{
if(Input.GetMouseButtonDown(0) && GlobalVars.haveKey == false)
{
// do nothing
}
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if (hit.transform.name == "door" && GlobalVars.haveKey == true)
{
Destroy(this.gameObject);
}
}
}
}
}
key.cs (Key script)
using UnityEngine;
using System.Collections;
public class key : MonoBehaviour {
void Start()
{
Update();
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if (hit.transform.name == "key")
{
GlobalVars.haveKey = true;
Destroy(this.gameObject);
Debug.Log("worked");
}
}
}
}
}
Pixelation.cs (screen pixelation script, requires a render texture on both the script and camera)
using UnityEngine;
using System.Collections;
public class Pixelation : MonoBehaviour {
public RenderTexture renderTexture;
void Start() {
int realRatio = Mathf.RoundToInt(Screen.width / Screen.height);
renderTexture.width = NearestSuperiorPowerOf2(Mathf.RoundToInt(renderTexture.width * realRatio));
Debug.Log("(Pixelation)(Start)renderTexture.width: " + renderTexture.width);
}
void OnGUI() {
GUI.depth = 20;
GUI.DrawTexture(new Rect(0,0, Screen.width, Screen.height), renderTexture);
}
int NearestSuperiorPowerOf2( int n ) {
return (int) Mathf.Pow( 2, Mathf.Ceil( Mathf.Log( n ) / Mathf.Log( 2 ) ) );
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612165624im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Displaying render texture with semi transparency 0 Answers
CEFGlue with Unity 1 Answer
Mesh Texture Not Rendered On Edges on iOS 0 Answers
Selectively apply material via RaycastHit 0 Answers
Wrong Coloring (Sometimes) when capturing "Screen"-shot 1 Answer