Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 gpinto · Nov 04, 2016 at 08:19 PM · c#arrayprefabsmathsine-wave

How to move prefabs array in sine wave?

I'm rather new to C# and Unity, so appologize if the question is too basic.

I have an array of prefabs instantiated and I'd like to move them like a sine wave. Attached it's how it looks so far (they've only been created).

alt text

I reckon each prefab would need to be in a different position of the wave, so maybe there'd be a delay of their values?

Any help would be appreciated. Thanks

 using UnityEngine;
 using System.Collections;
 
 public class SineWave : MonoBehaviour {
 
     public int numberOfDots = 28;
     public GameObject[] waveDots;
     public GameObject waveDotPrefab;
 
     private object sineWave;
 
     void Start()
     {
         // re-initilaze array to get correct size
         waveDots = new GameObject[numberOfDots];
 
         // instantiate all prefab clones
         for (int i = 1; i < numberOfDots; i++)
         {
             waveDots[i] = Instantiate(waveDotPrefab, new Vector3(0, 0, i), Quaternion.identity) as GameObject;
         }
     }
 
     void Update()
     {
         // move the prefab clones as a sine wave
         for (int i = 1; i < numberOfDots; i++)
         {
             // this was just part of my tries and fails
             //waveDots[i].transform.position = new Vector3(0, 0, 0);
         }
 
     }
 
 }


sine-wave.png (57.9 kB)
Comment
Add comment
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

2 Replies

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

Answer by Hellium · Nov 04, 2016 at 08:26 PM

Try this :

   Vector3 position = waveDots[i].transform.position ;
   position.x = Mathf.Sin(Time.time + i *factor) * amplitude ;
   waveDots[i].transform.position = position ;

Where factor is a floating value you will have to choose to make your sine wave smooth or not. A small value will make the wave smoother but with a low "frequency".

Comment
Add comment · Show 5 · 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
avatar image gpinto · Nov 06, 2016 at 02:51 PM 0
Share

Thanks! But that makes the spheres move in the x axis only. I'm having issues in moving them like that ugly black wave I've drawn. They're already instantiated in a row in the z axis, so that should be enough to move them in the x axis, I'd guess.. =/

alt text

answer.png (37.1 kB)
avatar image Hellium gpinto · Nov 06, 2016 at 03:07 PM 0
Share

You are right, I edited my answer. It should work now :)

avatar image gpinto Hellium · Nov 06, 2016 at 04:41 PM 1
Share

Got it! Thank you for all the help! (:

Show more comments
avatar image
2

Answer by frederik99 · Nov 04, 2016 at 09:44 PM

like this?

 //scales the wave vertically
 public float amplitude;
 //scales the wave horizontally 
 public float waveLength;
 //shifts the wave
 public float phase;
 
 void Update() {
         
     for (int i = 1; i < numberOfDots; i++) {
 
         float normalizedProgress = (float)i / (numberOfDots - 1);
         float x = normalizedProgress * waveLength + phase;
         Vector3 position = (Vector3.forward * i) + Vector3.up * Mathf.Sin(x) * amplitude;
     }
 }
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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Examples on how to populate an array with prefabs 1 Answer

Distribute terrain in zones 3 Answers

A problem with intersection detection 1 Answer

What's a more efficient way to sort an array into multiple groups? 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