Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wenbert · Jan 24, 2013 at 08:53 PM ·

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;
         }
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jan 25, 2013 at 04:47 PM 0
Share

I'm confused about what you are asking. Is the issue calling spline.GetPositionOnSpline() every update?

avatar image wenbert · Jan 28, 2013 at 10:47 PM 0
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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() {
 
     }
     
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cloud recognition in Vuforia 0 Answers

Change Camera 3 Answers

How connect from Unity WebPlayer app to C# server 1 Answer

Virtual Tour of a monument (scripting) 1 Answer

ASSet store models can be used in my game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges