Finding one of the several positions of a moving GameObject

Hi again peeps. Quick question: How do I find and record into a floating variable the position of a moving object?

Here’s my JS code:

var gel : Transform;
var on = false; 
var speed : float = -20;
var chDir = false;
var release = false;
var start = true;

function OnTriggerEnter (pipCollider) 
{
	if(pipCollider.gameObject.name == "Pip")
	{
    	on = true;
    }
}

function Update() 
{
		if(start)
		{
			gel.transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
		}
		
		if(on)
		{
			start = false;
			var pivot = pip.position;
			gel.transform.RotateAround (pivot, Vector3.forward, 50 * Time.deltaTime);
			chDir = true;
		}
		else if (!on){}
		if(chDir && Input.GetButtonDown("Fire1"))
		{
			on = false;
			release = true;
		}
		if(release)
		{
			var pivotor = pip.position;
			var orbitor = gel.position; //here lies the problem - we want a snapshot of the gel position not the uptodate one
			var vect = (orbitor-pivotor).normalized;
			var newSpeed = speed*vect;
			gel.transform.Translate(newSpeed * Time.deltaTime);
		}
}

In the if(release) statement, the orbitor variable is set to the position of the gel Transform object. However, this variable changes over time and I want the orbitor to remain a fixed snapshot of the gel position at the moment the mouse is clicked.

For clarity: I want to put snapshot values into the orbitor variable that do not change after the object has moved. I also want to be able to swap that value for another snapshot later on.

Any thoughts?

You are doing this right here since you are storing the position in a Vector3. Vector3s are structs which get a copy of data. If you were saving the transform, then you would be saving a reference to the data, which would then change every frame as transform.position changed.

The reason your value is changing every frame is that you set release to ‘true’ so your code block on lines 38 - 42 will get executed every frame. One way to fix it is to move it into the block above. Input.GetButtonDown() only returns true for a single frame, so if you move lines 39 and 40 inside of the ‘if’ block on lines (lines 32 - 35), the positions will only be saved once. The second problem is scoping. You are declaring the variable inside of and ‘if’ statement inside of Update(), so it gets reset every Update() call. So assuming orbitor is the position you want to save, the code would look like:

At the top of the file:

var orbitor : Vector3;

and then inside Update:

   if(chDir && Input.GetButtonDown("Fire1"))
   {
     on = false;
     release = true;
     orbitor = gel.position; 
   }