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 Image(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
  Lots of Allocs in Editor Update (Poor Performance in Editor) TdayMFG 4 120 02-06-2020, 10:13 PM
Last Post: _Aka_
  Curvy not appearing in Unity 2019.2.14f1 bmacdonald1986 3 365 12-12-2019, 11:40 AM
Last Post: _Aka_
  Lag in editor, trying to optimize it UsernameHed 2 417 10-10-2019, 08:04 AM
Last Post: UsernameHed
  Unable to import in 2018.4.2 LTS unity romit99 1 711 06-25-2019, 11:56 AM
Last Post: _Aka_

Forum Jump: