how can i read the movement of a transform the same way i would a mouse axis ?

how can i get a readout of the movement of an object ? so far i can get the position of an object as a vector but i only want to know by how much it is moving so that i can add this to an other object ,a mouse controled cursor ,so that it dosent get left behing when the player moves

1 Answer

1

I don't know if there's a better way, but one way would be to save the position then subtract it on subsequent Update calls, for instance, and get the velocity that way.

var lastPos : Vector3;
var lastVelocity : Vector3;

function Start()
{
  lastPos = transform.position;
  lastVelocity = Vector.zero;
}

function Update()
{
  deltaPos = transform.position - lastPos;
  lastPos = transform.position;
  direction = deltaPos.normalized;
  velocity = deltaPos * Time.deltaTime;
  deltaVelocity = velocity - lastVelocity;
  acceleration = deltaVelocity * Time.deltaTime;
  .. and so on..
}

i tought something like that would work but i ad no real idea of how to do it , thanks , i just hope this way wont be too heavy since it has to be called every frame

–

That shouldn't bog you down at all. It's calls like GameObject.Find and Mesh.Optimize that take a lot of time, avoid those in Update

–

i think it would be better to calculate from the position in the last frame instead of checking only at start ill try to find a workaround

–

Yikes! That was my intent, and I'd forgotten it. Code corrected above.

–

great i got the whole thing working but i took a whole different approach , i just took its current position , added to that a float variable that goes up acording to mouse movement

–