- Home /
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); }
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.
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);
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.
thanks jtsmith1287 it worked out.ill make sure of your advice too next time
Would you $$anonymous$$d selecting this as the accepted answer please? :)
Your answer
Follow this Question
Related Questions
Vector3 sets to different coordinates than specified 1 Answer
Issue's With Positioning 0 Answers
Offset against Rotated Object,Applying Offset to a Rotated Transform 1 Answer
Reposition to minimum distance from target 2 Answers
Play animation as the value changes from 0 to 1 with changing GameObject's position in unity 0 Answers