• 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
1
Question by Arishtanemi1998 · Sep 11, 2014 at 05:41 PM · transformpositionvector3coordinates

split vector3 coordinates in to x,y,z

hey i need to get the x,y and z values of the coordinates separately so that i can modify them and get the new coordinates for position. can u tell me hoew to implement that in javascript.the code that i am currently using, displays the coordinates of a moving object.i need to separate them and modify them like x+4 or y-3 and then use them in transform.position.thanks in advance

my current code:- //Unity will constantly check the position of the GameObject.function Update () {

//Creates a variable to check the objects position.

myPosition = transform.position;

//Prints the position to the Console.

Debug.Log(myPosition); }

Comment
Add comment · Show 2
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 gjf · Sep 11, 2014 at 03:27 PM 0
Share

1) please format your code.

2) Vector3 has 3 components (shocker) called x, y & z ... depending on what you want to do, try something like:

 Debug.Log(myPosition.x);

this is surprisingly helpful.

avatar image tanoshimi · Sep 11, 2014 at 05:46 PM 0
Share

Also, if you want to modify the transform.position of x+4, y-3 as in your example, you don't need to access the seperate components - try this:

myPosition = transform.position + Vector3(4, -3, 0);

1 Reply

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

Answer by jtsmith1287 · Sep 11, 2014 at 09:31 PM

First off, make sure to keep your posts formatted. People don't want to read code when it just looks like sloppy plain text.

Second, the solution is super simple.

 myPosition.x += 4
 myPosition.y -= 3

You can also add multiple Vector3 objects together.

 newPos = Vector3(4, -3, 0);
 myPostion = transform.position;

 finalPosition = newPos + myPosition;

or

 finalPosition = transform.position + Vector3(4, -3, 0);


I would also encourage you to spend time reading through documentation on the Unity wesbite. A quick server for Vector3 would have shown you everything I just posted.

Comment
Add comment · Show 2 · 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 Arishtanemi1998 · Sep 12, 2014 at 01:37 PM 0
Share

thanks jtsmith1287 it worked out.ill make sure of your advice too next time

avatar image jtsmith1287 · Oct 16, 2014 at 09:22 PM 0
Share

Would you mind selecting this as the accepted answer please? :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

transform.position = new Vector 3 NOT moving to correct position? 1 Answer

Vector3 add or subtract 5 Answers

Vector3.Slerp not working 2 Answers

Issues with local position and positions 1 Answer

how do i fix my script in the content below? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges