Get direction vector

Hey guys, My brain isn’t working, slightly embarrassed here with what I thought would be easy

Please take a look at the image attached.

So I’m trying to bet the direction vector from another vector, So in this case the red dot to the orange square, the pinkish line shows the direction im trying to get. I thought that would just be vectorA - vectorB, but that doesn’t work either.

Anyone know the solution?

thanks

1660140--103799--Screen Shot 2014-06-11 at 19.28.01.png

if… you are at 5 and you want the direction to 10, all you have to figure out is this:

target - origin = direction

Which means, square - circle = direction.

Now, if you are trying to do this using vertices and such from a mesh. You are going to have to account for some factors here. Scale, rotation, or whatever. Luckily, they provided a wonderful function called TransformPoint.

transform.TransformPoint(vertex) - transform.position = direction

2 Likes

Hey,

Thanks dude. This is for a mesh yes, how would you do it without transform then? Since the circle is just an array of vector3s
Can I still use transform point?

Vertex = TransformPoint(Vertex*) - directionPoint | something like this?*

Depends if you are working in some type of scale.

If you have a mesh, you are going to have a transform. If all of this is just simply a bunch of Vector3’s then you will not have a scale. The question is: Is this list of Vector3’s common to a position and/or rotation? If so, that means pretty much that they are going to follow an object.

All being said. Yes, you can still use a transform. All you would have to do is to create a single object in that space, rotation and scale as the original, it doesn’t even have to have a mesh or anything.

So lets assume that you have an object at 10,10,10 and that has a scale of 2 and some odd rotation. (just to say its a funky object.) And we want to get the position from a mesh, with it’s own position scale and rotation. To do this, we simply convert a point from the local orientation of the mesh to world space and then back to the local space of the new object.

Vector3 point = meshTransform.TransformPoint(vertex);
Vector3 local = object.InverseTransformPoint(point);

Now, because the local object is at whatever place that we are at “local” is a direction already.

// both of these specifically state that they are affected by scale. :wink:

(this part is not really specific to your needs)

Next, if you have a direction already.

Vector3 direction = meshTransform.up;
Vector3 local = object.InverseTransformDirection(direction);

This now converts the up rotation of the mesh to a world direction, then converts it back to a local direction using the object as a reference. (this means that if an object is facing “north” and the other object is facing “down” that the final result of this, is that the transform actually faces towards the rear of the other object.)

// both of these are NOT affected by scale, since direction is not scalable like this.

I might be over thinking this.

Basically what I’m trying to do is this:

When the mouse is clicked, modify the vertices or vector3’s in they’re in the direction from the mouse (red dot), Am I just complicating things?

I’m good with the logic for determining which vertices to move etc. Just need to figure out the direction vector

When I do vetex Position - mouse position it looks like this; which isn’t correct

erm… that would be something like this:

Plane plane = new Plane(Camera.main.transform.forward, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float dist;
if(plane.Raycast(ray, out dist){
   Vector3 mouseClick = ray.GetPoint(dist);
   Vector3 pos = transform.TransformPoint(vertex);
   Vector3 normal = (pos - mouseClick).normalized;
   pos += normal * 0.2f;
   vertex = transform.InverseTransformPoint(pos);
}

Basically, get the click is the big thing, which comes from the camera ray of that point a plane on that object. either way, your going to have to get that click point.

Next, get the normal direction, and multiply it by the distance you need, then flip it back to local space.