Getting coordinates of a point on a Spline
Asked Answered
N

2

0

Hi people, beginner programmer here.

I’m using the Unity Splines package for the first time I’m trying to Instantiate() something on a specific point on a spline.

How can i get the coordinates (transform) of a point that’s, for example, at three quarters of the length of a spline, or at the very start of the spline?

Nerin answered 9/12, 2023 at 10:50 Comment(0)
S
0

There doesn’t seem to be a convenient, built-in method to do that.
I guess the best solution is to find a user-defined method that makes it easier for you.

First, read Venkify’s example. It’s a lot of code for something that should be much easier (posted on Apr 14, 2021):
https://forum.unity.com/threads/how-to-move-an-object-along-a-spline-spriteshape.837739/#post-7036966

Then check out the helper method that Brogan89 created (on the same page, last edited on Apr 2, 2023):
https://forum.unity.com/threads/how-to-move-an-object-along-a-spline-spriteshape.837739/#post-7747068

Brogan89’s method lets you do this:

Vector2 startPosition = spline.GetPoint(0.0f);
Vector2 midPosition   = spline.GetPoint(0.5f);
Vector2 endPosition   = spline.GetPoint(1.0f);

The spline variable should be of the type Spline or a type that is derived from the Spline class, and of course it should be a reference to an existing spline.

I hope this helps.

Socinian answered 9/12, 2023 at 10:50 Comment(0)
S
0

Hi @Nerin ! Regarding your demand, You can have a look at the API documentation to check all the methods available to you :slight_smile: but for your case, when creating a SplineContainer with splines you then have access to : mySplineContainer.Evaluate() method and that will give you the evaluated position, tangent (forward direction), and up of a spline point.
In your case you can do:
mySplineContainer.Evaluate(0.75f, out var position, out var tangent, out var up) and you should have all the necessary information

Stauffer answered 10/7 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.