How can I save the distance to 3 or more objects from 1 object, and move that same object back to the exact same distance from the objects when I load the game all without getting the vector3 of the objects?
without getting their location, how do you save their location? That’s a tricky one! ![]()
The only possible scenario that I can imagine that could make sense, though it was unwritten by you… is if the 3 objects can only be in 1 direction and you only want “how far” away they are.
Is that the case?
The other objects move when I update the location to compensate for the float point persesion error, and there all around the universe to become anchor points of reference for saving object positions because there vector3 might not be the same when the player logs back into the game.
Okay. So, I re-read your original message and your second one. You’re using objects to compensate for floating point errors? And are these other points moving while the player is logged out, etc?
Just trying to follow along what’s happening in your head…
If you want the saved object’s location to always remain relative to an object, you can … save it’s Vector3 offset ( I am still unsure why you want to save without that.)
Could you explain a little better the precision error that mentioned that you wanted to avoid?
I’m moving the anchor points and everything in the world except the player ship. If the player moves far enough and drops off an object than logs out or something and comes back he comes back to vector3(0,0,0) but the object say for example was dropped off at vector3(0,1,0) or something because its real close to the player no matter where the player moves. What I want is the object to be not in relation ship to the player in vector3 space but in relationship to the other objects in the scene, like the anchors. No matter where the player aka (moves) he is always at vector3(0,0,0) so I don’t know how to save an object to a location in world space that would stay there as the player flys away.
Why you would want to move everything except the ship is a little strange to me. However, f you drop something off, and continue to move around, does that dropped object move with everything else? Probably, right? So when the player logs off, you can save the dropped object’s position at that time.
Yeah but lets say the player was really far from where he started when he first logged in, than logged off. When he logs back in he will be back at the start as will be the object he left behind.
You’re right, I made a mistake there.
This is part of the reason that moving everything but the player sounds confusing to me.
Why not move just the player, wouldn’t that be much easier? ![]()
A work-around I can think of is to keep track of how far the ship has moved, despite always being at (0,0,0) and when you drop/do anything for which you want the position to be saved, in that manner, you record that tracked position for the object’s saved records.
Edit: So you’ve moved (10,0,0) but appear as (0,0,0), you drop an object, store its ‘SavedLocation’ variable as (10,0,0) and when you logout, record that value for reload later ![]()
Well that might work, but I wouldn’t have any idea how to even do that. Have you ever played Eve Online? I am trying to make a space game like that with the vastness of space. If you don’t move everything except the player the player starts to shake real bad at like a few thousand meters but with the way I did it you can be millions of meters away from where you started and it wont shake at all.
Shaking, eh? I wasn’t familiar with that; I had not tried such vast distances.
That sounds like another problem, though.
To your response, well, what values do you give the world to move to mimic the player moving?
Do you say : move(1,0,0) makes everything move (-1,0,0) ? That’s what you’d save (-1,0,0)
Just have to keep adding as you go and you’d “carry” the result with you, to be used anytime you want to drop/save a location in the wild?
I’m tempted to give the distance issue some thought, but I don’t want to theorize without tests/proof and if you like the way your game is working… that’s cool
So, I’ll leave it at that for now.
Give me a few minutes here and I’ll post the script I’m using.
Okay.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class UniverseOffset : MonoBehaviour
{
public Transform player;
public Transform universe;
public int xSector = 0;
public int zSector = 0;
public float sectorSize = 1000f;
private Transform tempUniverse;
void Awake()
{
tempUniverse = new GameObject(universe.name + "_2").transform;
tempUniverse.position = tempUniverse.eulerAngles = Vector3.zero;
}
void Update()
{
Vector3 pos = player.position;
int xMove = 0;
int zMove = 0;
if (Mathf.Abs(pos.x) > sectorSize)
xMove = Mathf.FloorToInt(Mathf.Abs(pos.x) / sectorSize) * ((pos.x > 0) ? 1 : -1);
if (Mathf.Abs(pos.z) > sectorSize)
zMove = Mathf.FloorToInt(Mathf.Abs(pos.z) / sectorSize) * ((pos.z > 0) ? 1 : -1);
if (xMove != 0 || zMove != 0)
{
xSector += xMove;
zSector += zMove;
Vector3 offset = new Vector3(-xMove * sectorSize, 0, -zMove * sectorSize);
universe.position += offset;
Transform tmp = tempUniverse;
List<Transform> childs = universe.OfType<Transform>().ToList();
for (int i = 0; i < childs.Count; i++)
{
childs[i].parent = tmp;
}
tempUniverse = universe;
universe = tmp;
tempUniverse.position = Vector3.zero;
// only needed when the player is not inside the universe gameobject itself
//player.position += offset;
}
}
}
Okay, I can’t 100% soak in all of that code right away, but I do have some thoughts.
If the universe is a transform with all of its objects as children , and you want to move it , keeping the player steady, can you not just move it all at once, by moving the universe’s transform?
Another note, though unrelated if the first thing I said is true, is that children transforms are already in an array.
You can access them by : transform.childCount (you needn’t make a new list of them, in other words.)
So, if the first part of my response is possible, just keep track of how much you’ve moved the universe. If for any reason it’s not good, or has some side effects that you do not like, then keep track of this “offset” variable you have created there in your code.
Something like:
Vector3 accumOffset;
// later on in update
accumOffset += Vector3 offset = new Vector3(-xMove * sectorSize, 0, -zMove * sectorSize);
// when you drop/want to save something:
objToSave.GetComponent<SaveScript>().savedPosition = accumOffset;