- Home /
[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;
}
}
}
}
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 ? ;-)
Your answer
Follow this Question
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