Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating splines at run time
#1
Hi,

I'd like players to be able to place stations and then as a simple step at this early stage, have a spline automatically drawn between the stations at run time.

Should this be fairly easy to accomplish? Just work out the vector between point A and B and make it create a spline?

How can I then have it generate a mesh on this? I've had a play with the generator which is very powerful but obviously at run time I'd need a saved template to be accessed and used to create railway track, can this be done in some way?

Thinking ahead though, where I'd like players to lay track how they like, would it be possible to create prefabs of sections of track (straights, curves etc) with a spline and control point on each gameObject, then lay these down on a grid system with the pieces of track creating a joined spline when placed next to each other? Or would you go about it a different way where they draw the track in a similar way to the "paint splines" example?

Sorry for so many questions, once I'm off in a direction that will work hopefully I won't need to keep bothering you Big Grin  Thanks again.
Reply
#2
Creating a spline at runtime:
CurvySpline.Create
CurvySpline.Add

Creating a generator at runtime:
Just make a prefab and instantiate it
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
Thanks so much, this is brilliant and working, one question though, is there a way I could show the spline without adding a control point so the player can see what the spline will look like before they click to place the control point? Would I need to add then remove the control point in the Update function continuously to achieve this effect? Or is there a better way that's perhaps less cpu intensive?

For anyone in the future the code to add a train track / road etc in runtime where you click the mouse is below, I just added an empty game object, put a CurvySpline component on it, I used a generator from the train demo that I had edited a bit (make sure the input spline paths are set to the new game object you made) and then add this script to the game object. Now I can make train tracks like in an sim/tycoon type game!

Code:
using UnityEngine;
using FluffyUnderware.Curvy;

public class TrackMaker : MonoBehaviour
{
    public CurvySpline track;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                track.Add(hit.point);
            }
        }
    }
}
Reply
#4
Adding and removing each frame a CP for preview is not the best way. A better way is to add the preview CP once, and then update it's position each frame.

If you want still want to completely avoid adding a CP, you can the interpolation methods directly (like CurvySpline.Bezier(...)), but then you will have to understand the maths behind that to use it properly. If you still want to do that, check how those methods are used in CurvySpline's public Vector3 Interpolate(float localF, Space space = Space.Self)
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#5
Hi and thanks so much again that's a great idea and it's working, only issue is frame rates are dropping to 10fps from 400+, is this likely because it's applying the full generated mesh to it? I tried to run it in a coroutine with 0.1 second delay but it's still slow. I wonder if I need 2 generators one for building like this with a really simplified mesh (or no mesh?) and the second is then applied once the track is "layed"? Or do you think it'll still be slow just because of the calculations and the mesh isn't the issue?

Code:
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (Input.GetMouseButtonDown(0))
            {
                //add the track
                track.Add(hit.point);
            }
            //move the next/last cp to mouse cursor position
            if (transform.childCount > 0)
            {
                transform.GetChild(transform.childCount - 1).gameObject.transform.position = hit.point;
            }
        }
 
Reply
#6
This can be helpful
https://curvyeditor.com/documentation/performancetips
And as always, the profiler is your friend.
Also, you can define in the generator's inspector how frequently it should update.
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#7
(07-16-2020, 08:02 PM)_Aka_ Wrote: This can be helpful
https://curvyeditor.com/documentation/performancetips
And as always, the profiler is your friend.
Also, you can define in the generator's inspector how frequently it should update.

Thanks it's much better now, and no doubt can be improved a lot more with some effort!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  snap to the curve created by the curvy splines segment points ShiroeYamamoto 0 0 5 hours ago
Last Post: ShiroeYamamoto
Bug Changing spline connection in inspector causes splines to revert to defaults lacota 3 6 03-18-2024, 07:55 PM
Last Post: _Aka_
  Using Unity's SplineContainer in Curvy Splines dlees9191 3 15 02-26-2024, 09:49 AM
Last Post: _Aka_
  Distance travelled across multiple splines jh092 1 7 02-23-2024, 09:44 AM
Last Post: _Aka_

Forum Jump: