Hey,
is it possible to store an object/prefab which is hit by a ray into a variable, so that i can change the objects transform to follow the mouse position. And if it is how i can do it?
Yep, of course:
Transform hitObject;
void PerformRaycast() {
if (Physics.Raycast(someRay, out RaycastHit hit)) {
hitObject = hit.transform;
}
}
So hitObject now got the transform of the hit object in scene. But how can i then change the transform of the hit object.
I tried this but it didnt work ![]()
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray2, out RaycastHit hit))
{
hit.transform = Input.mousePosition;
}
Once you have stored the hitObject variable you can modify it by assigning a value to hitObject.position
Ok thx i will try ![]()
Instead of randomly trying code, I suggest you type the title of this post into Google.
Otherwise you’re going to spin and spin and spin on what is like a 60-second problem.
For instance, in the second block of code, hit.transform is a read-only property, so you cannot assign to it.
For two, you generally cannot assign the mouse position directly to a transform and have it do anything meaningful. There are Camera component cast functions you can use.
Look up a tutorial, the time you save will be your own.