- Home /
Instantiate prefabs on top of a spline
Hello,
I am using SuperSpline (http://www.super-splines.de). I am having a hard time figuring out how to place "coins" on top of the spline.
Right now, I am using Update() to "run" through the Spline and get the points - but I am sure that this idea is not good.
How would I tackle this kind of problem? Can anyone point me to the right direction?
Thanks!
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
//This class computes a point on a spline that is as close as possible to a given gameobject
public class CoinTest : MonoBehaviour
{
public Spline spline;
public GameObject prefab;
public int numberOfObjects = 5;
public float passedTime = 0f;
public float speed = 1f;
public WrapMode wrapMode = WrapMode.Clamp;
void Update() {
passedTime += Time.deltaTime * speed;
var carPos = new Vector3(0f, 0f, 0f);
var position = new Vector3();
position = spline.GetPositionOnSpline( WrapValue( passedTime, 0f, 1f, wrapMode ) ) + carPos;
Instantiate(
prefab,
Vector3.Lerp(
new Vector3(0f,0f,0f),
position,
(passedTime)
),
Quaternion.identity
);
}
private float WrapValue( float v, float start, float end, WrapMode wMode ) {
switch( wMode )
{
case WrapMode.Clamp:
case WrapMode.ClampForever:
return Mathf.Clamp( v, start, end );
case WrapMode.Default:
case WrapMode.Loop:
return Mathf.Repeat( v, end - start ) + start;
case WrapMode.PingPong:
return Mathf.PingPong( v, end - start ) + start;
default:
return v;
}
}
}
I'm confused about what you are asking. Is the issue calling spline.GetPositionOnSpline() every update?
@robertbu I totally got the entire thing wrong. I am calling now calling it on Start() - I will create a new answer for it. Give me a few $$anonymous$$utes.
Answer by wenbert · Jan 29, 2013 at 12:40 AM
I managed to get the coins on top of the spline on Start(). The code below is not very good and it is still in it's early stages but it places coins on the spline as intended.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
//This class computes a point on a spline that is as close as possible to a given gameobject
public class CoinTest : MonoBehaviour
{
public Spline spline;
public GameObject prefabCoin;
public GameObject prefabCoinBig;
public int coinCount = 100;
public int coinsInEachGroup = 5;
void Start( )
{
int coinGroupCount = (coinCount / coinsInEachGroup)+coinsInEachGroup;
GameObject[] prefabs = new GameObject[2] {prefabCoin, prefabCoinBig};
System.Random rnd = new System.Random();
//Create an array of starting points
int[] coinStartPoints = new int[coinGroupCount];
for (int i = 0; i < coinGroupCount; i++) {
coinStartPoints[i] = (i+(i+coinsInEachGroup))*coinsInEachGroup;
//Debug.Log(coinStartPoints[i]);
}
//Debug.Log(coinStartPoints[0]);
for (int i = 0; i < coinCount; i++) {
double randomFloat = rnd.NextDouble(minCoinXOffset, maxCoinXOffset);
int index = rnd.Next(0,2);
int currentPointIndex = rnd.Next (coinGroupCount);
//Debug.Log(">>> coinStartPoints["+currentPointIndex+"]"+coinStartPoints[currentPointIndex]);
//put coins in succession. EG in 12345 in a straight line
for(int x=0; x<coinStartPoints.Length; x++) {
if(i == coinStartPoints[x]) {
for(int k=0; k<coinsInEachGroup; k++) {
cloneCoin(i+k, prefabs);
}
i = i + coinsInEachGroup;
}
}
}
private void cloneCoin(int i, GameObject[] prefabs) {
GameObject clone;
clone = Instantiate(
prefabs[0],
Vector3.Lerp (
spline.GetPositionOnSpline(0),
spline.GetPositionOnSpline(i / (float)coinCount),
1
) + new Vector3(0f,coinYOffset,0f),
Quaternion.identity
) as GameObject;
clone.transform.rotation = Quaternion.identity;
}
void Update() {
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613091531im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
Change Camera 3 Answers
How connect from Unity WebPlayer app to C# server 1 Answer