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

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!
Reply
#2
*scrubs head* Do you have a scribble or screenshot of this?
Reply
#3
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 Image(s)
   
Reply
#4
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.
Reply
#5
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.
Reply
#6
(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:

Code:
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()
      delta=player.Z-pos.z;
   }
} else {
// same as above, but with a negative stepsize
}
transform.position=pos;

Can be written more elegant, but should work.
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Move CP00X position in hierarchy UsernameHed 1 72 11-12-2019, 02:01 PM
Last Post: _Aka_
  Extrude custom mesh along a path CesarGenera 3 304 09-19-2019, 04:47 PM
Last Post: _Aka_
  Path not as smooth as spline jawadkhan.9026 3 759 06-12-2019, 11:19 AM
Last Post: _Aka_
  How can I make the path visible in game? PretzelStudios 4 816 06-02-2019, 09:01 PM
Last Post: _Aka_

Forum Jump: