- Home /
How do you set the initial value of a variable to an objects current position in unity?
I am scripting in c# and I can't seem to find a simple way to set the value of some variables I made, X, Y, Z to the position of 3 spheres I made in unity. I will be manipulating the position of the balls using the script and keybinds but when I run the program I want the balls to be where I put them in 3d space within unity, not at 0,0,0. I tried not assigning a value to them upon declaration and instead doing it under the start method because it won't allow me to use transform.position.x where the variable is declared (at the top just inside the class and before the first method). This gives an odd result where they seem to be in the right relative positions but not facing the same way relative to my camera, as if they were all rotated together. I suppose that's something to do with world vs local axis, but I shouldn't have to worry about that so I'm using the wrong strategy.
Thanks.
float throttle = 0f, count = 1;
public double mass = 1, throttleCoeff = 0.25, Y, X, Z, adtGravity = 0;
double gravityF = 0, Vy = 0, Vx = 0, Vz = 0, adtThrust = 0, accelywg = 0, accely1 = 0, accelx1 = 0, accelz1 = 0, accely2 = 0, accelx2 = 0, accelz2 = 0, accely3 = 0, thrustF;
// Use this for initialization
void Start ()
{
Y = transform.position.y;
X = transform.position.x;
Z = transform.position.z;
}
Answer by RetepTrun · Sep 10, 2012 at 01:46 AM
I'm not totally clear on what you're trying to do but maybe add some more variables to hold the rotation of the objects. RX=transform.rotation.x
Your answer
Follow this Question
Related Questions
Setting Position of Spawned Prefab 2 Answers
gravity causes transform.position not to work 0 Answers
Offset against Rotated Object,Applying Offset to a Rotated Transform 1 Answer
Is mousePosition not equal object.transform.position? 0 Answers
How to make object stay certain distance above ground (relative o ground normal) 1 Answer