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 MightyMatty · Jan 23, 2012 at 06:11 AM · instantiateorbitclones

Prefab issue - all appear at same location

Hi

I have a script which instantiates clones of a prefab nicely in a circle around a sphere. The prefabs are static and appear in a line at a set distance, with slight offsets to that it isn't a perfect line. I then have another script on the prefab which sets it off orbiting the sphere.

The problem I have is that when I instantiate 100 clones with the orbit script attached to the original prefab, all of them appear at exactly the same point from the sphere :) All 100 no longer have their own position, even though they do orbit.

How am I losing the nice positioning of the clones by adding the orbit script? It's late...maybe I'm missing something daft...code included below.

Instantiate Code:

 var asteroid : Transform;
 var asteroidCount = 100;
 var radius = 50.0;
 
 function Start () {
 
     for (i = 0; i < asteroidCount; i++){
         
         var angle = i * Mathf.PI * 2 / asteroidCount;
         var randomYPosition = Random.Range(-0.2, 0.2);
         
 //Set position of each ball.
 var pos = Vector3 (Mathf.Cos(angle), randomYPosition, Mathf.Sin(angle)) * radius;
         Instantiate(asteroid, pos, Quaternion.identity);
         
     }
 }

Orbiting code:

 var TimeMultiplier : int = 200000;
 
 var Name : String;
 
 var Parent : Transform;
 
 
 
 enum Orbit {Auto, Manual}
 
 var SetOrbit : Orbit;
 
 
 
 enum Rotation {Auto, Manual}
 
 var SetRotation : Rotation;
 
 
 
 enum Season {Auto, Manual}
 
 var SetSeason : Season;
 
 
 
 var NonPlanarOrbit : boolean;
 
 var TidalLock : boolean;
 
 var LockOrbit : boolean;
 
 var OrbitAngle : float;
 
 var KeepTime : boolean;
 
 private var ThisTransform : Transform;
 
 private var EarthDays : float = 365.242199;
 
 
 
 // Orbit Stats
 
 var OrbitalPeriod : float = 1.0; // Earth Years
 
 var OrbitalDistance : float = 2; // In Au
 
 var OrbitOffset : Vector2 = Vector2(0,0);
 
 var OrbitPosOffset : float;
 
 var OrbitStartPos : float;
 
 var OrbitYears : int;
 
 var OrbitDays : int;
 
 var OrbitHours : int;
 
 var OrbitMinutes : int;
 
 var OrbitSeconds : float;
 
 private var OrbitalTime : float;
 
 private var OrbitalDegSec : float;
 
 
 
 //Rotation Stats
 
 var RotationOffset : float;
 
 var RotationPeriod : float; // Earth Hours
 
 var RotationYears : int;
 
 var RotationDays : int;
 
 var RotationHours : int;
 
 var RotationMinutes : int;
 
 var RotationSeconds : float;
 
 private var RotationDegSec : float;
 
 private var RotationTime : float;
 
 
 
 // Planetary Stats
 
 var AxialTilt : float;
 
 var HoursInDay : int;
 
 var RotInOrbit : int;
 
 //Planet Counters
 
 var CounterYear : int;
 
 var CounterDay : int;
 
 var CounterHour : int;
 
 var CounterMinute : int;
 
 var CounterSecond : float;
 
 var CurrentOrbitPos : float;
 
 var OrbitOffSetYear : boolean;
 
 private var RotCounter : float;
 
 private var OrbCounter : float;
 
 
 
 function Start(){
 
 SetupPlanet();
 
 SetupOrbit();
 
 SetupRotation();
 
 
 
     if(OrbitOffSetYear){
 
         OrbCounter = OrbitPosOffset;
 
     }
 
     if(LockOrbit){
 
         KeepTime = false;
 
     }
 
 }
 
 
 
 function SetupPlanet(){
 
 ThisTransform = transform;
 
 ThisTransform.localEulerAngles.z = AxialTilt;
 
 }
 
 
 
 function SetupOrbit(){
 
     if(SetOrbit == 0){
 
         OrbitalTime = ((((EarthDays * OrbitalPeriod) * 24) * 60) * 60);
 
         OrbitalDegSec = (360 / OrbitalTime) * TimeMultiplier;
 
     }else{
 
         OrbitalPeriod = 0;
 
         OrbitalTime = ((((((((OrbitYears * EarthDays) + OrbitDays) * 24) + OrbitHours) * 60) + OrbitMinutes) * 60) + OrbitSeconds);
 
         OrbitalDegSec = (360 / OrbitalTime) * TimeMultiplier;
 
     }
 
 }
 
 
 
 function SetupRotation(){
 
     if(!TidalLock){
 
         if(SetRotation == 0){
 
             RotationTime = (((24 * RotationPeriod) * 60) * 60);
 
             RotationDegSec = (360 / OrbitalTime) * TimeMultiplier;
 
         }else{
 
             RotationPeriod = 0;
 
             RotationTime = ((((((((RotationYears * EarthDays) + RotationDays) * 24) + RotationHours) * 60) + RotationMinutes) * 60) + RotationSeconds);
 
         }
 
         RotationDegSec = (360 / RotationTime) * TimeMultiplier;
 
         RotInOrbit = Mathf.Round(OrbitalTime / RotationTime);
 
         HoursInDay = ((RotationTime / 60) / 60);
 
     }
 
 }
 
 
 
 function Update(){
 
 // Update Orbit Position
 
     if(!LockOrbit){
 
         var ODS : float = OrbitalDegSec * Time.deltaTime;
 
         OrbitStartPos += ODS;
 
     }
 
 
 
 var rot : Quaternion = Quaternion.Euler(0,OrbitStartPos + OrbitPosOffset, 0);
 
 var DesiredPos : Vector3 = rot * Vector3(0, 0, 1) + Parent.position;
 
 var DesiredDir : Vector3 = (DesiredPos - Parent.position).normalized;
 
 
 
     if(NonPlanarOrbit){
 
         var NPOP = Mathf.Cos(OrbitStartPos*Mathf.Deg2Rad+Mathf.Deg2Rad);
 
     }
 
 
 
 ThisTransform.position = (Parent.position + OrbitOffset) + (DesiredDir * OrbitalDistance) + Vector3(0,NPOP * OrbitAngle,0);
 
 
 
 // Update Rotation
 
     if(TidalLock){
 
         ThisTransform.LookAt(Parent);
 
         if(KeepTime){
 
             UpdateCounters(0, ODS);
 
         }
 
     }else{
 
         var RotDegSec : float = RotationDegSec * Time.deltaTime;
 
         if(KeepTime){
 
             UpdateCounters(RotDegSec, ODS);
 
         }
 
         ThisTransform.Rotate(0,RotDegSec, 0, Space.Self);
 
     }
 
 }
 
 
 
 function UpdateCounters(RotDegSec : float, ODS : float){
 
 
 
 //Count Orbits / Years
 
     if((OrbCounter + ODS) >= 360){
 
         CounterYear += 1;
 
         CounterDay = 0;
 
         OrbCounter = (OrbCounter + ODS) - 360;
 
     }else{
 
         OrbCounter += ODS;
 
     }
 
     
 
 CurrentOrbitPos = OrbCounter;
 
 
 
 //Count Days    
 
     if((RotCounter + RotDegSec)>= 360){
 
         CounterDay += 1;
 
         RotCounter = (RotCounter + RotDegSec) - 360;
 
     }else{
 
         RotCounter += RotDegSec;
 
     }
 
 
 
 var CurrentTime = (RotCounter / 360) * RotationTime;
 
 
 
 //Count Hours
 
 CounterHour = (CurrentTime / 60) / 60;
 
 
 
 //Count Minutes    
 
     if(CounterHour > 0){
 
         CounterMinute = (CurrentTime / 60) - (CounterHour * 60);
 
     }else{
 
         CounterMinute = (CurrentTime / 60);
 
     }
 
 
 
 //Count Seconds
 
     if(CounterHour > 0 && CounterMinute > 0){
 
         CounterSecond = CurrentTime - ((CounterMinute + (CounterHour * 60)) * 60);
 
     }else if(CounterHour > 0 && CounterMinute == 0){
 
         CounterSecond = CurrentTime - ((CounterHour * 60) * 60);
 
     }else if(CounterHour == 0 && CounterMinute > 0){
 
         CounterSecond = CurrentTime - (CounterMinute * 60);
 
     }else if(CounterHour == 0 && CounterMinute == 0){
 
         CounterSecond = CurrentTime;
 
     }
 
 }
 
 
 
 function ClampAngle (angle : float, min : float, max : float) {
 
     if (angle < -360){
 
         angle += 360;
 
     }
 
     if (angle > 360){
 
         angle -= 360;
 
     }
 
     return Mathf.Clamp (angle, min, max);
 
 }

Any pointers would be awesome. Cheers.

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
0

Answer by syclamoth · Jan 23, 2012 at 06:19 AM

You shouldn't have posted that entire orbit script. sscce.org - post only the parts that are relevant.

In any case, as far as I can tell the problem occurs because despite the fact that you instantiate them all in different places, the orbit script immediately overrides the original position! You need to have some way of initialising the orbit script with a 'start position', so that the asteroids all have the correct location.

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
avatar image
0

Answer by MightyMatty · Jan 24, 2012 at 02:34 AM

I understand what you mean about the script but I was in a sharing mentality and thought someone may stumble across it and find it useful.

I thought as much! The orbit script is where I need the magic to happen. I don't seem to able to find anything to help me out though - do you have any pointers to wiki advise or tutorials by any chance?

Thanks for taking a look.

Comment
Add comment · Show 1 · 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 syclamoth · Jan 24, 2012 at 02:38 AM 0
Share

Did you write that orbit script yourself?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer

Order of instantiated objects 0 Answers

Just curious, assigning a clone multiple times, how does it work internally? 1 Answer

How do I acces a specific GameObject within the terrain???? 0 Answers

How do I check the variable of a specific clone? 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