- Home /
Extremely Fast Travel
I have been messing around with simulating real life space distances, and scales. I solved floating point problems, but when I try to travel at real life light speed, my script cannot correct fast enough to avoid floating point problems. I am wondering if there is anyway to make this correction faster than every frame. Here is the script:
using UnityEngine;
using System.Collections;
public class Reposition : MonoBehaviour {
public float maxDist;
public GameObject[] sceneObjects;
void Awake () {
sceneObjects = GameObject.FindGameObjectsWithTag("Respawn");
}
void Update () {
Vector3 xpos = new Vector3(0, transform.position.y, transform.position.z);
Vector3 ypos = new Vector3(transform.position.x, 0, transform.position.z);
Vector3 zpos = new Vector3(transform.position.x, transform.position.y, 0);
if(transform.position.x > maxDist){
transform.position = xpos;
foreach(GameObject obj in sceneObjects){
obj.transform.position = new Vector3(obj.transform.position.x - maxDist, obj.transform.position.y, obj.transform.position.z);
}
}
if(transform.position.x < -maxDist){
transform.position = xpos;
foreach(GameObject obj in sceneObjects){
obj.transform.position = new Vector3(obj.transform.position.x + maxDist, obj.transform.position.y, obj.transform.position.z);
}
}
if(transform.position.y > maxDist){
transform.position = ypos;
foreach(GameObject obj in sceneObjects){
obj.transform.position = new Vector3(obj.transform.position.x, obj.transform.position.y - maxDist, obj.transform.position.z);
}
}
if(transform.position.y < -maxDist){
transform.position = ypos;
foreach(GameObject obj in sceneObjects){
obj.transform.position = new Vector3(obj.transform.position.x, obj.transform.position.y + maxDist, obj.transform.position.z);
}
}
if(transform.position.z > maxDist){
transform.position = zpos;
foreach(GameObject obj in sceneObjects){
obj.transform.position = new Vector3(obj.transform.position.x, obj.transform.position.y, obj.transform.position.z - maxDist);
}
}
if(transform.position.z < -maxDist){
transform.position = zpos;
foreach(GameObject obj in sceneObjects){
obj.transform.position = new Vector3(obj.transform.position.x, obj.transform.position.y, obj.transform.position.z + maxDist);
}
}
}
}
Answer by Juice-Tin · Feb 22, 2015 at 11:47 PM
If you need to do something faster that every frame (which is odd), you can put it in a for loop.
The frame will not progress until the for loop is done.
//Adds 1 per frame
//Runs code once per frame
Update(){
//code
number1 ++;
}
//Adds 1, twice, per frame
//Runs code twice per frame
Update(){
for(int i=0; i<2; i++){
//code
number1 ++;
}
}
But this means that the loop runs within the frame, so this would not make the calculations frame independent.
Your answer
Follow this Question
Related Questions
Can the insides of a gameObject affect its transform? 0 Answers
GameObject Scale as World Coordinates (Units)? 1 Answer
postion scale and rotation greyed out and not working 1 Answer
How can I attach a gameobject to another without changing its scale. 1 Answer
Render Extremely Far off Objects 1 Answer