Posts: 22
Threads: 7
Joined: Jul 2020
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
Thanks again.
Posts: 2,110
Threads: 92
Joined: Jun 2017
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 helps immensely. Thank you.
Available for freelance work—feel free to reach out.
Posts: 22
Threads: 7
Joined: Jul 2020
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);
}
}
}
}
Posts: 2,110
Threads: 92
Joined: Jun 2017
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 helps immensely. Thank you.
Available for freelance work—feel free to reach out.
Posts: 22
Threads: 7
Joined: Jul 2020
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;
}
}
Posts: 2,110
Threads: 92
Joined: Jun 2017
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 helps immensely. Thank you.
Available for freelance work—feel free to reach out.
Posts: 22
Threads: 7
Joined: Jul 2020
(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!