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
1
Question by joergzdarsky · Aug 23, 2014 at 01:06 PM · gameobjectsmultipletranslatelateupdate

[C#] Translate.position a lot of gameobjects at once (due to velocity limitations)

Hi,

I am implementing a space game in Unity3D similar to Kerbal Space Program (just for the fun/hobby, spacegame using "real" distances, procedural planets (still a long road to go) and oculus rift support).

I of course meet the same issues like in KSP, therefor I also implemented a floating origin approach for the camera position to get rid of the floating point precision issue which works fine. After adding a number of asteroid gameobjects in a believable distance and starting to fly by, I noticed a need another solution for velocities, as the common speeds of the unity physics engine are not enough for universe distances. So I looked at KSP presentation at Unite 2013 trying to understand how they did it.

Basically I think I understood their approach. If the ridigbody where the camera is applied to (so basically the spaceship) reaches a certain speed, the velocity vector3 is copied to a, say, "referenceframe" script (which is similar to the floating origin script) with a separate vector3 velocity value. At the same time the velocity is copied, the velocity of the ridigbody (spaceship) is set to zero again. This separate vector3 value is used to, when other than (0,0,0), reposition all objects around the spaceship (which has now zero velocity and is "not moving") each frame. So similar to floating origin, just for velocities.

This works, however I notice some stuttering when the script moves the objects.

Below is my script. One optimization might be to group the asteroid-gameobjects into one parent gameobject so that not 400 asteroid-gameobjects have to be moved separately by the script during LateUpdate(), but only one gameobject. Hopefully this increases performance, however any other hint how to reposition a lot of objects at once would be very much appreciated.

 // ReferenceFrameBylayer.cs
 // Written by Joerg Zdarsky
 // 23 August 2014
 using UnityEngine;
 using System.Collections;
 
 /* Reference Frame Script
  * Contents
  * 1 Description
  * 2 Usage
  * 3 Notes
  * 4 C# - ReferenceFrameByLayer.cs
  * Description
  * This script translates all objects in the world to keep objects moving around an object that has
  * velocity zero. Similar approach to floating origin, just for velocities
  * Usage
  * Attach this script to the gameobject that moves around 'threshold' determines the distance at which mass object translation occurs, 
  * this is not a light-weight operation so less frequent movement is better. 
  * Notes
  * Make sure your other camera controls are in Update() and not LateUpdate() otherwise they could clash.
  */
 [RequireComponent(typeof(GameObject))]
 public class ReferenceFrameByLayer : MonoBehaviour
 {
     public float threshold = 27777.78f; // =100.000km/h, treshhold is meters per second (as provided by rigidbody.velocity.magnitude)
     public Vector3 referenceFrameVelocity = new Vector3(0,0,0);
     public string layer;
     public GameObject parentGameObject;
  
     void LateUpdate()
     {
         Vector3 gameObjectVelocity = gameObject.rigidbody.velocity;
         float gameObjectMagnitude = gameObjectVelocity.magnitude;
         Vector3 gameObjectAbsoluteVelocity = gameObjectVelocity + referenceFrameVelocity;
         float gameObjectAbsoluteMagnitude = gameObjectAbsoluteVelocity.magnitude;
 
         // Check if velocity needs to be added to reference framce
         if (gameObjectMagnitude > threshold) {
             this.referenceFrameVelocity += gameObjectVelocity;
             gameObject.rigidbody.velocity = new Vector3(0,0,0);
         }
 
         // Check if velocity needs to be returned to the gameobject
         else if (gameObjectAbsoluteMagnitude < threshold && referenceFrameVelocity.magnitude >= threshold) {
             gameObject.rigidbody.velocity += this.referenceFrameVelocity;
             this.referenceFrameVelocity = new Vector3(0,0,0);
         }
 
                 // Moves objects (not-light-weight)
         if (referenceFrameVelocity != new Vector3 (0, 0, 0)) {
             Object[] objects = FindObjectsOfType(typeof(Transform));
             foreach(Object o in objects)
             {
                 Transform t = (Transform)o;
 
                 if (t.gameObject.layer == LayerMask.NameToLayer(layer))
                 {
                     t.position -= this.referenceFrameVelocity*Time.deltaTime;
                 }
             }
             
             objects = FindObjectsOfType(typeof(ParticleEmitter));
             foreach (Object o in objects)
             {
                 ParticleEmitter pe = (ParticleEmitter)o;
                 Particle[] emitterParticles = pe.particles;
                 for(int i = 0; i < emitterParticles.Length; ++i)
                 {
                     emitterParticles[i].position -= this.referenceFrameVelocity;
                 }
                 pe.particles = emitterParticles;
             }
         }
     }
 }
Comment
Add comment · Show 1
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 _dns_ · Aug 23, 2014 at 02:02 PM 0
Share

Hi, maybe you did this already: using a .net IL disassembler like ILSpy http://ilspy.net/ could give you information about how $$anonymous$$SP did it by looking into their code... (if not obfuscated). Hummm... I feel guilty to write that... but it's to help learning, which is a good cause, right ? ;-)

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Destroying objects with the same name that are touching eachother. 1 Answer

OnTriggerEnter() not firing for mutliple objects 0 Answers

How do I switch between multiple gameobjects during run time. 1 Answer

How to translate multiple selected gameObjects with exact numbers. 4 Answers

Multiple GameObjects with the same script all react but I only want one at a time. 2 Answers


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