Using transform.position.z to move an object

Hi guys,

I’m currently putting together a simulation where I know the beginning and end time and distances…

For this example lets say:

  • start time = 0
  • start distance = 0
  • end time = 10
  • end distance = 300

(this is a very simple example of my data which has multiple segments) - all my data is in a look up table, find the current time then lookup the current z location for the object

So, the math is working and I can calculate where an object needs to be at a certain time. At the moment I’m simply updating the transform.position.z value for the object in a FixedUpdate function to move it to the required z location.

My problem is that when two or more objects are being moved they begin to lag and the animation isn’t smooth - it’s like they get out of sync with one another. I need my objects to be at an exact distance at a specific time.

Is transform.position.z the right way to go? Does it have something to do with the screen update rate?

Cheers, Kymbo.

That should be in Update; FixedUpdate is for physics.

If you want to move it along the Z axis, yes.

You should always take the update rate into account for everything, using Time.deltaTime as necessary. Anyway the simplest way to use transform.position.z to move an object would be to use Lerp, preferably in a coroutine.

–Eric

Thanks for the quick response Eric - I can’t use Lerp because it’s linear interpolation as I understand it. My data is not linear.

Cheers, Kym.

The example you posted is linear. Knowing the beginning and end times and distances is pretty much the definition of linear.

–Eric

Hi Eric,

The data I provided was to illustrate a point - the real data is not linear and simulates an acceleration curve based on a number of known points across a number of segments.

Thanks, Kym.

you still lerp

Lerp (from : float, to : float, t : float)
from and to is start and end
t is the ratio
if, as you say, you have the math to extract the (non-linear) ratio at any given time, that is still t

Sorted :slight_smile:

The problem was that my data table did not have enough points. I thought that 60 a second would be plenty. Turns out not to be the case. I now have 1,000 points per second and it works smoothly for direct movement and LERP…

Cheers, Kym.