Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scene 51: Dynamic track between two points
#1
Hi,

I just started using Curvy and it's been great so far!

I'm trying to build a moving object (eg. a vehicle) that has a reference to two transforms - a previous location and a next location, and is able to dynamically travel from the previous loc to the next loc (which can be changed dynamically, eg. through another script). 

I have tried extending Scene 51 by modifying the logic in the E51_InfiniteTrack.cs script to direct the vehicle to the next location's position (the following code is in the addTrack method). 

Code:
// Get localPosition of last control point of spline
Vector3 p = TrackSpline.ControlPointsList[TrackSpline.ControlPointCount - 1].transform.localPosition;

// Calculate direction towards nextTransform
Vector3 targetDirection = (nextTransform.position - p).normalized;

mDir = Vector3.Lerp(mDir, targetDirection, 0.5f);

Vector3 position = TrackSpline.transform.localToWorldMatrix.MultiplyPoint3x4(p + (mDir * CPStepSize));

It works fine, however, the vehicle kept starting its movement from (0, 0, 0). Since I need it to start from a specific point, I figured I needed to modify the start CP's Vector3.zero in the setup method. I changed this to the position of the specific start point and also modified mDir, ie.

Code:
Vector3 startPosition = initialTransform.position;
mDir = (nextTransform.position - previousTransform.position).normalized;

// Add the start CP to the spline
TrackSpline.InsertAfter(
    null,
    startPosition,
    true
);

However, the vehicle's movement becomes erratic, although it does start from the specified point. And after moving in a random direction, it often gets stuck / bunched up in one spot. I'm not sure if it's due to some issues with the mDir. 

Would appreciate any help, and thank you in advance!
Reply
#2
Hi,

Glad you are liking Curvy Splines.

Various points:
  1. When adding the first control point of your spline, using InsertAfter or InsertBefore doesn't make sense, since their are no other control points. Prefer using the Add() method.
  2. When computing targetDirection, you substract two positions, one in local coordinates and the other in world coordinates. That's not good. Use world coordinates for all of them. Meaning:
    Vector3 p = TrackSpline.ControlPointsList.Last().transform.Position;
  3. In the following
    mDir = Vector3.Lerp(mDir, targetDirection, 0.5f);
    Use Vector3.SLerp instead of Lerp. Look for them online to understand why.
  4. About this line:
    Vector3 position = TrackSpline.transform.localToWorldMatrix.MultiplyPoint3x4(p + (mDir * CPStepSize));
    once again, it mixes positions/directions computed using a mix of local and world coordinates. Either compute everyting in local (and then convert to world) or in wold.About the conversion itself, you can use one of these instead, which are less prone to human error:
    • https://api.curvyeditor.com/FluffyUnderware.Curvy.CurvySpline.html#FluffyUnderware_Curvy_CurvySpline_ToWorldPosition_UnityEngine_Vector3_
    • https://api.curvyeditor.com/FluffyUnderware.Curvy.CurvySpline.html#FluffyUnderware_Curvy_CurvySpline_ToWorldDirection_UnityEngine_Vector3_
    • https://api.curvyeditor.com/FluffyUnderware.Curvy.CurvySpline.html#FluffyUnderware_Curvy_CurvySpline_ToLocalPosition_UnityEngine_Vector3_
    • https://api.curvyeditor.com/FluffyUnderware.Curvy.CurvySpline.html#FluffyUnderware_Curvy_CurvySpline_ToLocalDirection_UnityEngine_Vector3_
    Review your code with all what I said in mind, and you might find what mistakes you made when computing the positions/directions used by your script.

If and when you feel like it, please leave a review for the asset, that helps a lot.
Have a nice day
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#3
Thank you for the reply! Some of the code above were originally in the E51_InfiniteTrack.cs script, hence I left them in, but I've made the modifications you've suggested and it works now! Thank you so much.
Reply
#4
You are welcome.
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Disable rebuild of meshes in dynamic mode Zilk1 3 13 05-27-2024, 08:14 AM
Last Post: _Aka_
  snap to the curve created by the curvy splines segment points ShiroeYamamoto 3 11 04-02-2024, 02:24 PM
Last Post: _Aka_
  Get position of all control points for a spline gekido 1 9 03-28-2024, 10:08 PM
Last Post: _Aka_
  Does CurvySplines support displaying handles and points during runtime? niuage 1 8 12-11-2023, 12:01 PM
Last Post: _Aka_

Forum Jump: