I have a rendertexture of a model viewer for items in our game, this rendertexture renders a camera below the scene which has a model (cube for testing) and obviously the camera.
When testing (before it was added as a rendertexture to the UI), I was using the OnMouseDrag function on the cube to spin it in world space with the mouse, but of course that wonโt work anymore
Can anyone think of a way to do this through a rendertexture or do something similar, like with a raycast or something?
If I understand your question correctly, you want to rotate the cube by clicking on the cube that is rendered on your screen through render texture?
if you, easiest way I could think of is to just retrieve the dimensions of the place where the cube is rendered ,and then just check your inputs on screen.
โ when input happens on screen
โ am i within bounds ?
โ yes, just do a little drag logic here ( drag delta input etc )
โ rotate cube in scene based on values returned
I already have a script for rotating the box (see below):
using UnityEngine;
using System.Collections;
public class ObjectRotate : MonoBehaviour
{
public float torque = 10.0f;
private float baseAngle = 0.0f;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void OnMouseDrag()
{
rb.AddTorque(Vector3.up * torque * -Input.GetAxis("Mouse X"));
rb.AddTorque(Vector3.right * torque * Input.GetAxis("Mouse Y"));
}
}
Which works on the collider position on the mesh (as I understand it); I want that same effect but through the rendertexture.
This seems to be what I am looking for, but this is the first time I have even touched rendertextures, I have no idea how to do most of that, or how OnMouseDrag would receive the input.
Youโll have to calculate a movement delta between frames when the user clicks in the bounds of the render texture and then rotate the camera being used as the render source based on that delta. So:
on mouse down check if in render texture; if yes store mouse position
next frame subtract current mouse position from store position to get change in mouse position