Trying to move an object towards another's axis.

32984-vectormaf.png

Elaboration: I have a need for objects to move towards an object’s axis, but not its position. I have been trying to think of how to accomplish this for about an hour now but I’m missing something. I just can’t crack it.

The catch is that the target object could be at any rotation; normally my thought would be to simply tell the attracted objects to just move towards a new Vector3 that has the X and Z positions of the target but the Y of the attracted object, but of course this breaks down once you rotate the target object to no longer be aligned to the world.

Could be something simple I’m not thinking of. Any help appreciated.

Let’s say I have a script on one of the cubes and it references the ‘moveTo’ object. And I want to move to the line defined by transform.up of the ‘moveTo’ object. Using Vector3.Dot(), I can calculate the closest point on the axis to my position and move to that position:

#pragma strict

var moveTo : Transform;
var speed = 1.5;

function Update() {
	//Calculate closest point from transform.postion
	// to the up axis of 'moveTo'
	var v = transform.position - moveTo.position;
	var t = Vector3.Dot(v, moveTo.up);
	var pos = moveTo.position + t * moveTo.up;

	transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}