Home Game Development unity – How do I calculate the route of an object relative to the one other object’s view

unity – How do I calculate the route of an object relative to the one other object’s view

The easiest method to do that can be:

``````Vector3 localPoint = bigCircle.rework.InverseTransformPoint(
smallCircle.rework.place
);
``````

This `native` variable now holds the place of the small circle inside the huge circle’s native coordinate system. That is, a coordinate system the place (0, 0, 0) is the origin of the large circle, +z factors to the large circle’s “ahead” route (blue arrow of the native transformation gizmo), +x factors to its proper (pink arrow), and +y factors to the circle’s up (inexperienced arrow).

If you then need to get a signed angle within the huge circle’s horizontal aircraft, that would appear to be:

``````float angleDegrees = Mathf.Atan2(localPoint.x, localPoint.z) * Mathf.Rad2Deg;
``````

That provides you with a bearing angle the place 0 = straight forward / “N”, 90 = to the suitable / “E”, 180 = precisely behind / “S”.

Since you say you need to calculate this with math, not use a pre-built perform, what `InverseTransformPoint` is doing is taking `bigCircle.rework.localToWorldMatrix` and inverting it to get `worldToLocalMatrix`, then multiplying the offered level by this matrix:

`````` Vector3 localPoint = bigCircle.trandform.worldToLocalMatrix
* smallCircle.rework.place;
``````

You can get the same outcome with out matrix math like so:

``````// Remove bigCircle's translation.
Vector3 worldOffset = smallCircle.rework.place - bigCircle.rework.place;

// Remove bigCircle's rotation.
Quaternion antiRotation = Quaternion.Inverse(bigCircle.rework.rotation);
Vector3 localOffsetScaled = antiRotation * worldOffset;

// Remove bigCircle's scale (if relevant).
Vector3 antiScale = bigCircle.rework.nativeScale;
antiScale = new Vector3(1f/antiScale.x, 1f/antiScale.y, 1f/antiScale.z);
Vector3 localOffsetUnscaled = Vector3.Scale(localOffsetScaled, antiScale);
``````