How to project a vector 3 locally ? How to get a local projection of a Vector 3?

My game object is child moving and rotating locally. It contains a Vector 3 “A”
Given Vector3 “A”, I wish to make the projection locally. My world forward in this example is “B” . “C” is a simple projection of “A” in local axis and is what I’m looking for.

How to project a vector 3 locally ?

Vector3 desiredDir;
transform.TransformDirection(desiredDir);
1 Like

@Laperen I was not able to understand how it works. I try your code also but my transform is not rotating. Can you expand the code using finalProjection_C desiredDir_A etc. I will use it in FixedUpdate()

public Vector3 vector_A, projection_C;

void FixedUpdate() {
    vector_A= new Vector3(4f, 5f, 11f);
    projection_C = transform.TransformPoint(vector_A);
}

Is just an example. Probably I’m to tired. I go to sleep and return tomorrow fresh!
Probably I need to use Transform.TransformDirection as you mention.

If this is exactly what you have, you have a huge typo among many other typos.

//yours
transform.ransformPoint(A);
//proper
transform.TransformDirection(A);

@Laperen I wish to get the angle but I still unable to get “C”. Can you expand ?

Maybe I didn’t understand you correctly.
It might be easier to explain what you are going to use this for, rather than that you are attempting to do.

Are you just trying to get the local rotation and position of a transform?

transform.position //the position in the world
transform.localPosition; //the position relative to its parent
transform.rotation; //the rotation in the world
transform.localRotation; //the rotation relative to its parent

Now we are talking! Yes, the calculation and the given is in local space. @makeshiftwings Is better to use the position or rotation for making the projection?

maybe you all mean that is not possible to make the local projection and that is necessary to convert it world space?

Even if you calculate the local projection, I’d say you still have to use it in relation to world space. If I assume you are going to shoot a ray using this projection with lets say a local rotation of 20 degrees, it needs to be converted to world space rotation to be used in Raycast. The same is true for using coordinates in Linecast. Local position (10,10) will need to be in world coordinates to be made use of.

1 Like