Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Position in path relative to Z world axis

I'm trying to achieve a gameObject that follows the path on the X and Y axis, but is linked to the world Z axis position of another object.

The main player have forces moving him on Z, and it is not following any path. He doesn't rotate, just moves on X and Y by position. I need enemies to stay in the path, and take their Z position from the player, so they are in front of the player, or at the same position.

Is this possible? When I try to link them it doesn't work because the path is not straight, and its length is greater than the world z position.

Thanks for the help!
*scrubs head* Do you have a scribble or screenshot of this?
Yeah, it's a bit difficult to explain with words... I attached a screenshot of the Unity editor. The player is the one with the axis shown. The capsule object is the "enemy" following the spline.
The track (squares) follows an almost straight structure along the Z axis. The player has his movements splitted between a 2D space (X and Y) and the 3rd dimension is Z, used to accelerate and decelerate (somewhat like and old racing game that did only have left and and right, and accelerate or brake).
The camera is just behind the player.

The enemies that follow the path should be related to the Z position of the player.

I hope this makes sense!! Big Grin

Thanks Jake

Attached Files Thumbnail(s)
In other words you want to find the spline position that matches the same Z "depth" as the player, right? The best solution for this depends on the number of enemies, i.e. you should test different approaches for their performance. Ideas:

- Alter the enemy until the Z-delta (between player<->enemy) is near zero. If you use InterpolateFast() you'll only do lerps, so this should be reasonable fast. Sort of brute force approach
- Use GetNearestSplineTF() to get the spline point nearest to player. Could get wrong results depending on the curve, though. To speed it up, check the segments bounds and use GetNearestSplineTF only against those segments within near distance

I would stick to the first idea. You can further optimize it by iterating manually over the cached points (stored in CurvySplineSegment.Approximation[]) and compare their Z, then lerp between the two nearest points.
In other words you want to find the spline position that matches the same Z "depth" as the player, right?
That's it! Smile

I tried both ideas you suggested.

-Using GetNearestSplineTF() worked quite well, just a bit of delay between movements. However it hits performance. This is what I have in FixedUpdate:
float currentPosition = ASpline.GetNearestPointTF (player.transform.position);
followSpline.Position = currentPosition;

How could I check the segments bounds only to use the near ones?

-Alter the enemy until the Z-delta (between player<->enemy) is near zero.
I'm struggling with this (I don't know that much about scripting in general, and how the curvy code exactly works). However it seems a much faster option with what I made so far:

First try, just adding some value to the FollowSpline position in fixed update:
if (transform.position.z - player.transform.position.z < 0)
            followSpline.Position += moveFactor;

Resulting in a not so smooth movement.

2nd try, 
if (transform.position.z - player.transform.position.z < 0)
            followSpline.Position += ASpline.InterpolateFast (moveFactor).z;

Resulting in a jaggy movement, as moveFactor is a fixed value...

How could I improve this to make a smooth movement?

Thanks a lot! Really.
(02-08-2015, 05:30 PM)'socoman3' Wrote: How could I check the segments bounds only to use the near ones?
In 1.61 you'll need to calculate the bounds manually, but calling GetNearestPointTF() each frame kills performance anyways.

About the delta approach: Setting position directly of course produce jerks because this way you skip deltatiming. Perhaps try to alter the speed, the higher the delta, the the speed, if delta is almost 0, set the speed to 0. Though depending on your players speed the movement may feel too laggy.

Another (maybe simpler) idea:

Create a custom script and in fixed loop try something like:

float delta=player.Z-transform.position.z;
if (Math.Approximate(0,delta))
return; // nothing to move
Vector3 pos;
float tf;
if (delta>0){
   float step=0.02f; // maybe inc/dec stepsize based on delta...
   while (delta>0){
      pos=spline.MoveFast(ref tf, step,...); // if MoveFast() is too inaccurate, increase cachesize or use Move()
} else {
// same as above, but with a negative stepsize

Can be written more elegant, but should work.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rasterized Path Range issue proton 7 26 04-30-2024, 11:17 AM
Last Post: _Aka_
  Incorrect mesh alignment after extrusion on curved path Thinkurvy 10 21 04-17-2024, 10:57 AM
Last Post: _Aka_
  Getting Distance from a World Point zorksox 3 5 04-16-2024, 07:30 PM
Last Post: _Aka_
  Get position of all control points for a spline gekido 1 6 03-28-2024, 10:08 PM
Last Post: _Aka_

Forum Jump: