Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Gilead7 · Aug 11, 2016 at 04:17 PM · c#savejsontranslate

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!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
Comment
Add comment · Show 3 · Share
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 Gilead7 · Aug 11, 2016 at 04:44 PM 0
Share

How would you cache the direction if not in the x coord? So when would you use translate?

avatar image DiegoSLTS Gilead7 · Aug 11, 2016 at 04:59 PM 0
Share

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.

avatar image Gilead7 · Aug 11, 2016 at 05:06 PM 0
Share

Thanks! In my case, transform.position works.

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

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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