# Calculate vector3 global point projecting it in local space using Unity and C#?

How to project a vector 3 locally ? Unity has a special function for this?

For example, if a has a GameObject that contains 2 children Vector A (transform) "My A Arrow" and Vector C (transform) "My C arrow projection".

I wish a C# solution script for "C" vector (transform) so that it can be used in Unity Game engine.

Just for example: My game object "A" is child moving and rotating locally. I wish to make the projection locally. "B" is my world forward in case is needed. "C" is a simple projection of "A" to the plane in local axis.

Is simple to get the vector in world global axis, just taking x (x = 0f). But I fail locally, since my parent GameObject is rotating.

Attempted solution. public Vector3 vector_A, projection_C;

vector_A= new Vector3(4f, 5f, 11f);

void FixedUpdate()
{
projection_C = transform.TransformPoint(vector_A);
}

I try using transform, rotation, vector3 unity functions and quaternion. My skill was not at the level to understand wich function to choose for the task reference: https://docs.unity3d.com/ScriptReference/Vector3.html

I was able to get the solution but was so complex that I was thinking that Unity has a function that makes the local projection.

This is my attempt to describe the process:

First you need to make a clone of the game object, rotate it to world global and move it to origine 000. The child vector will folow the perent game object. Now you can make the projection of "A" taking out x. The result "C" must be rotated and move by the original game object rotation and position.

• Set the x coordinate to 0. Commented Mar 22, 2017 at 0:22
• C seems to be a copy of A but with the x coordinate set to 0. What is B for? Commented Mar 22, 2017 at 0:56
• The idea here is to improve the question. just edit it. I'm trying to make a simple projection locally. What is exactly the piece of information that is not clear. I do not know how to make it and I was not able to accomplish by myself using the quaternion, vector3 functions and the transform rotation unity functions. I fail understanding wish to choose since I'm new to the subject. Commented Mar 22, 2017 at 0:58
• Yes: C seems to be a copy of A but with the x coordinate set to 0. and B is the forward direction of the world in case is needed, The global direction, since this is a representation of local axis. Commented Mar 22, 2017 at 1:17
• Would transforming your vector A by your local frame transform, then set x = 0 then re-transform that vector by the inverse of your local frame transform do the trick? Commented Mar 22, 2017 at 1:30

Let's say you have a trasnformed coordinate system defined by a quaternion rotation, and vector3 position. For example, the transformed coordinate system may represent the orientation and position of an object in your game.

You can use Matrix.TRS to get a transformation matrix that represents this coordinate system. It would be done as follows:

Matrix4x4 m = Matrix4x4.TRS(position, rotation, new Vector3(1, 1, 1));

Note: new Vector3(1, 1, 1) is the identity scaling vector, it means no scaling is applied.

Once you have the matrix, you can use it to convert points from the transformated coordinate system to wolrd coordinates:

Vector3 trasnformedPoint = m.MultiplyPoint3x4(point);

You may convert from world to the trasnformed coordinate system by using the inverse of the matrix:

Vector3 point = m.inverse.MultiplyPoint3x4(trasnformedPoint);

Alternatively, if you are using an scene object, you can use its Transform to get the same results.

Vector3 trasnformedPoint = transform.TransformPoint(point);

And in the opposite direction:

Vector3 point = transform.InverseTransformPoint(trasnformedPoint);

Once you have the point represented in the coordinate system you need it, you can proceed to manipulate it any way you need, and you can transform it back to the coordinate system you had it to begin with.

For example, we can transform a point, remove a component, and transform it back:

Transform transform = gameObject.transform;

// ...

Vector3 trasnformedPoint = transform.TransformPoint(point);
trasnformedPoint.x = 0;
Vector3 newPoint = transform.InverseTransformPoint(trasnformedPoint);
• This Answers is in javascript and not C# Commented Mar 22, 2017 at 2:44
• @SoaringCode var is C#. Commented Mar 22, 2017 at 2:52
• (var) is not a standard Unity variable. Use Vector3, float and try to include the nomenclature that is in the example so that is more clear in relationship with the answer. In particular "point" , "trasnformedPoint " variables. Commented Mar 22, 2017 at 3:01
• @SoaringCode updated to use explicit types, I didn't expect that to be a issue with unity. Commented Mar 22, 2017 at 3:04
• @SoaringCode, var is C#. It is also used in Unity scripts (not to be confused with UnityScript). Please note that Unity is not the only implementation of C#, and is certainly not the governing source for what is C#. Theraot, var has no issue in Unity. In fact, some of the official scripts do use it. Commented Mar 25, 2017 at 9:20