- Home /
Position Translation problem
When saving the game (to a JSON string) I have it save the coordinates of the player and the map they are on. The First Person Controller has the tag of "Player"
var Location = new JSONObject
{
{"Map", Application.loadedLevel},
{"X" , playerPos.x},
{"Y" , playerPos.y},
{"Z" , playerPos.z}
};
Load in the saved Data...
Map=(int)PlayerObject.GetObject("Location").GetNumber("Map");
Debug.Log("MAP "+Map);
CoordX=(float)PlayerObject.GetObject("Location").GetNumber("X");
Debug.Log("X" + CoordX);
CoordY=(float)PlayerObject.GetObject("Location").GetNumber("Y");
Debug.Log("Y" +CoordY);
CoordZ=(float)PlayerObject.GetObject("Location").GetNumber("Z");
Debug.Log("Z" + CoordZ);
Everything looks good at this point. Then I wrote a function that will translate the player to the saved coords
public void PositionPlayer(float CoordX, float CoordY, float CoordZ)
{
Debug.Log("Send the Player to previous coordinates!");
Vector3 ReturnCoord;
ReturnCoord.x=CoordX;
ReturnCoord.y=CoordY;
ReturnCoord.z=CoordZ;
playerObject.transform.Translate(ReturnCoord);
}
In this case, I saved in front a large landmark, so I can make sure it worked properly. It seemed like it worked, only I was translated somewhere else, not where I expected to be. Where did I mess up? Also What is the best way to get the right level loaded and still get the right coordinates? Thanks!
Answer by DiegoSLTS · Aug 11, 2016 at 04:40 PM
Translate with only one parameter moves the object in "Self" space instead of World space, so the facing direction matters. Also, when using Translate the current position matters too, since translate actually adds the second parameter to the current position.
If you want to set a position then just set the position. I mean:
playerObject.transform.position = ReturnCoord;
How would you cache the direction if not in the x coord? So when would you use translate?
With facing direction I meant the rotation of the object, which deter$$anonymous$$es the "forward", "right" and "up" of any object.
Setting the position is the simplest way of doing what you want, but if you want to use Translate you have 2 options:
$$anonymous$$ake sure that the "forward" of the player is the same as the "forward" to the world, that the scale of the player is (1,1,1) and that the player is at (0,0,0), so Self space of the player matches World space.
Use Space.World as the second parameter of Translate ins$$anonymous$$d of the default value Space.Self and also make sure the player is at (0,0,0).
Translate is used when you want to move a certain amount on any axis relative to the current position and direction. Imagine you have a Spaceship should always move forward at a certain speed ("speed" being a parameter in the same script that moves the Spaceship), then you can do:
transform.Translate(new Vector3(0,0,speed))
So, you can set an absolute position with transform.position, a position relative to the parent with transform.localPosition and a position relative to it's current position with Translate.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Inserting JSON and saving new values 1 Answer
how do i slowly translate a object to a other objects position 2 Answers
Unity Saved Serialized Levels 0 Answers