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 lnker · Jun 14, 2011 at 03:42 AM · parentchildmove

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....

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

2 Replies

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

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.

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

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);
         }        
 }

}

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

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

2 People are following this question.

avatar image avatar image

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

Calling parent variables from child and visa versa 1 Answer


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