Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active July 18, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/98abbf54f92ae6df3a0f5f9cee96c2ee to your computer and use it in GitHub Desktop.
Save tsubaki/98abbf54f92ae6df3a0f5f9cee96c2ee to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RaycastWithRenderTexture : MonoBehaviour
{
[SerializeField] Transform cursor = null;
[SerializeField] Camera rtCamera = null;
RaycastHit hit;
[SerializeField] float offset = 0.3f;
void Update ()
{
var inputPos = Input.mousePosition;
var viewportPos = new Vector2(inputPos.x / Screen.width, inputPos.y / Screen.height);
var ray = rtCamera.ViewportPointToRay(viewportPos);
if( Physics.Raycast(ray, out hit)){
cursor.localPosition = hit.point + hit.normal * offset ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment