- Home /
Move point in the direction of another
Hello there, guys!
I have a question... I've got cube for example, it's have 8 verts and him transform.position point is center inside.
I want to move every vert in direction of central point but in random distance. My thinking assumed st like that:
mesh = //here component of cube's mesh
for(var vert in verts)
{
var direction : Vector3 = vert - cube.transform.position;
direction = direction.normalize;
vert += direction * RandomDistance;
}
But don't know why it's calculate wrong direction... As result it's move every vert to wrong direction. I have no idea what should I do... Any help?
Regards.
Need more information. What did you expect would happen? What actually happened that wasn't supposed to?
Well, vert is moving, so that's ok, but in some other direction. Always in the same, I mean not random for every vert. They aren't going to center of cube and not to the 0,0,0 point. I don't really know how to define their direction because it's calculate wrong so...
The vertex points are in local-space, but the cube position is is world-space.
for(var vert in verts)
{
var direction : Vector3 = vert;
direction = direction.Normalized();
vert += direction * RandomDistance;
}
If you want to use the cube as a reference :
var direction : Vector3 = cube.transform.position - transform.position;
No - it's not answer alucardj, cuz I tried
cube.position - vert
cube.position - transform.TransformPoint(vert)
vert - cube.position
transform.TransformPoint(vert) - cube.position
End all of those badly calculate this damn direction.
Answer by Orzel · May 15, 2014 at 05:37 PM
I did it on my own. With regards for everyone who has the same problem:
direction = Target.transform.position - transform.TransformPoint(vert);
direction = moveDir.normalized;
vert = transform.InverseTransformPoint(transform.TransformPoint(vert) + (direction * distance));
Your code here is taking the long way around. There is no need for most of it and @alucardj's example is correct, and your 'direction' will be 'vert'. Here is a concrete example. Say your vert is (1,1,1), and your target.transform.position is (2,2,2). This means that the world position of the vert would be (3,3,3). So your calculation first converts (1,1,1) to (3,3,3) using Transform.Point(), then subtracts (2,2,2) to get back to (1,1,1). If you don't believe me, do:
Debug.Log(direction + ", " + vert);