- Home /
Finding a Vector3 from Quaternion, current Vector3 and Distance
I need to use a custom raycast implementation (from the MLAagents framework) and want to find the Vector3 location of where the raycast has hit. The information I have is the Quaternion rotation of the gameobject transmitting the ray, this gameobject's Vector3, and the distance between where the raycast has hit and this gameobject. I'm struggling to find a solution.
On another answer I found this:
public static Vector3 RayCastHitLocation(Quaternion rotation,Vector3 position, float distance)
{
Vector3 direction = rotation * Vector3.forward;
return position + (direction * distance);
}
However, as I increase the distance from a hit object, the resulting Vector3 changes, which shouldn't occur as the raycast is still fixed on the same spot.
Would appreciate any help. Thanks
The lines you have are standard and correct. Something else must be wrong. In theory being further away could introduce larger "rounding" errors, but they'd be too tiny to notice.
I agree with @Owen-Reynolds. Is it possible that the rotation is relative to the raycasting gameObject, in which case it perhaps shouldn't be Vector3.forward but rather the raycasting GameObject's forward transform?
Thanks for the replies! How would I get the gameobject's forward transform?
Here is a more fleshed out example of the code
public class TestScript
{
public void GetSensorStuff()
{
// this is a GetComponent<> in the real code
// RayPerceptionSensorComponent attached to child gameobject of agent gameobject
// This object is rotated to move sensor around
RayPerceptionSensorComponent3D rayComponent = new RayPerceptionSensorComponent3D();
var lengthOfRayOutputs = RayPerceptionSensor
.Perceive(rayComponent.GetRayPerceptionInput())
.RayOutputs
.Length;
var rayDistances = new float[5];
List<float[]> rayBuffers = new List<float[]>()
{
new float[(2 + 2) * lengthOfRayOutputs],
new float[(2 + 2) * lengthOfRayOutputs],
new float[(2 + 2) * lengthOfRayOutputs],
new float[(2 + 2) * lengthOfRayOutputs],
new float[(2 + 2) * lengthOfRayOutputs]
};
var rayOutputs = RayPerceptionSensor
.Perceive(rayComponent.GetRayPerceptionInput())
.RayOutputs;
for (int i = 0; i < 5; i++)
rayOutputs[i].ToFloatArray(2, 0, rayBuffers[i]);
// add just the distances to a new float array which represents all the ray cast distances
var distances1 = rayBuffers[0][3];
var distances2 = rayBuffers[1][3];
var distances3 = rayBuffers[2][3];
var distances4 = rayBuffers[3][3];
var distances5 = rayBuffers[4][3];
// I want to convert these distances into Vector3's
// assu$$anonymous$$g this script is attached to gameobject the rays are cast from
RayCastHitLocation(
transform.rotation,
transform.position,
distances1.ReverseNormalise(rayComponent.RayLength)
);
}
public static Vector3 RayCastHitLocation(Quaternion rotation, Vector3 position, float distance)
{
Vector3 direction = rotation * Vector3.forward;
return position - (direction * distance);
}
}
That's a lot of code, so a lot of things to check (if you know what values they should have -- I sure don't). But at the end, the call to RayCastHitLocation appears to do nothing since there's no "x=" in front. In other words, you don't even even use RayastHitLocation, so it can't be the problem.
Your answer
Follow this Question
Related Questions
How to make Camera position Independent of its Rotation? 1 Answer
Lerp in direction to certain distance 3 Answers
How can i place subjects in different positions? 0 Answers
Make object appear within camera view 0 Answers
Moving child and parent 1 Answer