Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set a speed in Unity Editor
#1
Hello.
I bought your Curvy in Unity Asset Store recently.
This tool is very useful for me.
Thank you.
And I am making some paths for a enemy in my game.

I have a basic question.

I would like to set a speed to each path point.


For instance, there are four path points in 01_MetaData.
And I would like to set a speed like below.
First path point -> speed: 1.0
Second path point -> speed: 2.0
Third path point -> speed: 3.0
Fourth path poin -> speed: 2.0

I have checked some previous questions in this forum.
It looks like that if we want to set a speed to a path point we have to write some code in UserAfterUpdate().
But I found that there are many parameters like Meta CG Options.


So, I have some questions.
1, Should I write some code in UserAfterUpdate() to control a path speed ?
2, Can I control a path speed with Meta CG Options?
3, Are there any good way to set a speed to a path in Unity Editor. Because our artists have to set a speed in Unity Editor. (They don't want to edit any code.)


Thank you.


Attached Files Thumbnail(s)
   
Reply
#2
(01-25-2018, 10:14 AM)akira_sh512 Wrote: This tool is very useful for me.
Thank you.

You are welcome

(01-25-2018, 10:14 AM)akira_sh512 Wrote: 1, Should I write some code in UserAfterUpdate() to control a path speed ?

If your enemies will simply follow the splines, then an approach similar to the one in the "01_MetaData" example scene should be enough.

Here is some code:

First, the SpeedMetaData class. Very similar to HeightMetadata from the example:
Code:
public class SpeedMetaData : CurvyMetadataBase, ICurvyInterpolatableMetadata<float>
{
    [SerializeField]
    float m_Speed;

    public object Value
    {
        get { return m_Speed; }
    }

    public object InterpolateObject(ICurvyMetadata b, float f)
    {
        var mdb = b as SpeedMetaData;
        return (mdb != null) ? Mathf.Lerp((float)Value, (float)mdb.Value, f) : Value;
    }

    public float Interpolate(ICurvyMetadata b, float f)
    {
        return (float)InterpolateObject(b, f);
    }
}

then, the spline controller that uses the SpeedMetaData. Also, similar to the MetaDataController class from the example
Code:
public class TestSplineController : SplineController
{
    /// <summary>
    /// This is called just after the SplineController has been initialized
    /// </summary>
    override protected void UserAfterInit()
    {
        SetSpeed();
    }

    /// <summary>
    /// This is called just after the SplineController updates
    /// </summary>
    override protected void UserAfterUpdate()
    {
        SetSpeed();
    }

    private void SetSpeed()
    {
        // Get the interpolated Metadata value for the current position (for SplineController, RelativePosition means TF)
        // If values can't be interpolated (no next value), current value (if present) or default type value (for float that's 0) is returned
        Speed = Spline.InterpolateMetadata<SpeedMetaData, float>(RelativePosition);
        Debug.Log(Speed);
    }
}

With those two classes, you will get variable speed for your SplineController.

Even if you don't use a spline controller, you can still use the spline's speed meta data. For example, the following code allows any object to retrieve the speed from the nearest spline point.

Code:
  float currentTf = Spline.GetNearestPointTF(transform.position);
  float speed = Spline.InterpolateMetadata<SpeedMetaData, float>(currentTf);
  //SetSpeed(speed);

So this way, you can have the enemies move freely (outside of the spline) but still modify their speed based on the data in a spline. This can be used as a level design tool for example.

(01-25-2018, 10:14 AM)akira_sh512 Wrote: 2, Can I control a path speed with Meta CG Options?

MetaCGOptions, like HeightMetaData and SpeedMetaData all inherit from CurvyMetaDataBase. I don't see any gain to set the speed in MetaCGOptions instead of using your own metadata class.

(01-25-2018, 10:14 AM)akira_sh512 Wrote: 3, Are there any good way to set a speed to a path in Unity Editor. Because our artists have to set a speed in Unity Editor. (They don't want to edit any code.)

Like the Height in the example scene, the speed will be settable via editor. The artists can add a SpeedMetaData to any control point, and set it's value in the editor.

Let me know if you need further explanations.
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 your replay.

I would like to try it.
Maybe it is very helpful.

Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Unity's SplineContainer in Curvy Splines dlees9191 3 14 02-26-2024, 09:49 AM
Last Post: _Aka_
  Unity 2021.2 Overlay System nehvaleem 5 17 12-15-2023, 10:09 AM
Last Post: _Aka_
  Curvy Splines Built In Editor Undo causes issues. Lupos 1 9 10-02-2023, 08:52 AM
Last Post: _Aka_
  Can Curve Spline be used for an in game level editor. Lupos 30 71 06-03-2023, 10:58 AM
Last Post: _Aka_

Forum Jump: