- Home /
How Can I move child object just in z axis cordinates?
I have Object "obj1" which have child("obj2")... "Obj1" moves thru X axis with "obj2".... Now I need to move "obj2" thru Z... For Example:
Global Coordinates: obj1(0,0,0), Obj2(0,0,0)
I need make a move of two this objects to targetPoint(5,0,25), and they must move togather and comes to this point intime., In cycle will be a lot of targetPoints... Tell me please how can i change for obj1 just X coords, and for it's child just Z coords....
Answer by aldonaletto · Jun 14, 2011 at 04:11 AM
You have two position properties, the transform.position and the transform.localPosition. position indicates the global coordinates of the object, while localPosition is the position relative to its parent. If you move obj1, obj2 will be moved too. You can move obj2 alone relative to obj1 at any time by changing its localPosition.
Answer by lnker · Jun 14, 2011 at 11:44 AM
Okay, thx, but.... Am I fool? Or smthing wrong with my code? Why does it start to move when I press "ReadFromFile" Button? Tell me please is it my mistake or Unity doesn't work 100%rightly with JavaScript??? object must get (2,0,2) coordinates, but it walk to next point 125*(0,0,3)125 when the MoveToFirst comes 125true*125...
[Java Script]
import System.IO; private var coordinates : String = "xyz.txt"; var pointCount; var xx : Vector3; var yy : Vector3; var dist : float; var dist2 : float; var t : float;
///////////////////////////////////////////////////
private var Regularspeed = 2; private var helper_var = 1; var movenpart: Transform; var movenpart2: Transform; var Dummy1 : Transform; var Dummy2 : Transform; private var counter : int;
///////////////////////////////////////////////////
var x = new Array(); var y = new Array(); var sx = new Array(); var sy = new Array(); var moveToFirst : boolean; var startCuttin : boolean; var finalstep : boolean;
///////////////////////////////////////////////////
function OnGUI () { if (GUI.Button(Rect(20, 25, 100, 30),"ReadFromFile")) { moveToFirst = false; startCuttin = false; finalstep = false; var sr = new StreamReader(Application.dataPath + "/" + coordinates); var fileContents = sr.ReadToEnd(); sr.Close();
var lines = fileContents.Split("\n"[0]);
pointCount = int.Parse(lines[0]);
for(var i = 1; i < pointCount+1; i++){
if(i%2){
x[i/2] = float.Parse(lines[i]);
}
else {
y[(i-1)/2] = float.Parse(lines[i]);
}
}
}
if (GUI.Button(Rect(20, 75, 100, 30),"TurnON")) {
moveToFirst = true;
}
}
function Update() { movenpart.transform.position = Dummy1.transform.position; movenpart2.transform.position.x = Dummy2.transform.position.x; movenpart2.transform.position.y = Dummy2.transform.position.y; movenpart2.transform.position.z = Dummy1.transform.position.z;
if (moveToFirst) { var ss1 = Mathf.Abs(x[0] - Dummy1.transform.position.z); var ss2 = Mathf.Abs(y[0] - Dummy2.transform.position.x); var xx1 = Vector3(0,Dummy1.transform.position.y,x[0]); var yy1 = Vector3(y[0],Dummy2.transform.position.y,0); print(y[0]); var firstDist = Vector3.Distance(Dummy1.transform.position, xx1); var firstDist2 = Vector3.Distance(Dummy2.transform.position, yy1); Dummy1.transform.position = Vector3.Lerp (Dummy1.transform.position, xx1, ss1 Regularspeed Time.deltaTime / firstDist); Dummy2.transform.position = Vector3.Lerp (Dummy2.transform.position, yy1, ss2 Regularspeed Time.deltaTime / firstDist2); } if (Dummy1.transform.position == xx1 && Dummy2.transform.position == yy1) { startCuttin = true; moveToFirst = false; counter = 1; }
if (startCuttin) {
var dist111 = Vector3.Distance (Dummy1.transform.position, Vector3(0,0,x[counter]));
var dist222 = Vector3.Distance (Dummy2.transform.position, Vector3(y[counter],0,0));
Dummy1.transform.position = Vector3.Lerp (Dummy1.transform.position, Vector3(0,0,x[counter]),Mathf.Abs(x[counter]-x[counter-1])*Regularspeed*Time.deltaTime / dist111);
Dummy2.transform.position = Vector3.Lerp (Dummy2.transform.position, Vector3(y[counter],0,0),Mathf.Abs(y[counter]-y[counter-1])*Regularspeed*Time.deltaTime / dist222);
//print("Counter = " +counter + " dist111=" + dist111 + " dist222" + dist2);
if(counter < (pointCount/2 -1) && dist111 ==0 && dist222 == 0) {
counter++;
//print("Counter = " +counter + " dist111=" + dist111 + " dist222" + dist2);
}
}
}
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Make Explosion Child of Target 1 Answer
Get Children of Child and Deactivate/Activate on Demand 1 Answer
Get Parent name of child 1 Answer