- Home /
 
Parenting a child to move on only one Axis !
Hello hello
I am parenting a child to move on only one of it's axis while the parent moves on all three...
I have an unwieldy paddle in my game, when I touch the child object and pull a trigger, I only want the child to move only up and down on it's own Y axis. While my parent paddle is moving loosely in all directions, it only effects the child's Y
    void OnTriggerStay (Collider col)
     {
 
 // Both the child and Parent have colliders 
 // When I contact  but the object together and press button :
 
         if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
         {
             col.attachedRigidbody.isKinematic = true;
 //  I need something like col.gameObject.transform.position.y = parent.gameObject.transform.position.y;
 // or
 //  col.gameObject.transform.SetParent(gameObject.transform.y );
         }
 
         if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
         {
             col.gameObject.transform.SetParent(null);
             col.attachedRigidbody.isKinematic = false;
         }
 
 
 
               Any ideas? Thanks ~be
Answer by Dibbie · Jun 27, 2016 at 06:13 AM
Where you have your comments in Line 10, replace it with this kind of idea:
 Vector3 limitedPaddle = col.gameObject.transform.position; //get the current position of your object you want to move up and down, im assuming thats the "col" object.
 
 //limitedPaddle.y = //howeverYouWantToManipulateTheYAxis;
 //Only y will be affected this way.
 
 col.gameObject.transform.position = limitedPaddle; //Now you are applying the full Vector3/Position to this object, but since you never changed X and Z, they stay the same, and only Y was changed, therefore, only Y updates on the object when you apply/set the objects position to 'LimitedPaddle'.
 
              Excellent ! I definitely need to finesse (as I am in VR) but this was exactly what I was looking for !
Thank you!
~be
Answer by $$anonymous$$ · Jun 27, 2016 at 07:03 AM
Do the objects need to be parented? If not:
 public class ObjectTracker : MonoBehaviour 
 {
     GameObject target = null;
 
     bool triggerDown, triggerStay, triggerUp;
     
     // Update is called once per frame
     void Update () 
     {
         //Checks if the trigger is down and the target is currently within range
         //Replace with your own controller code
         if(triggerStay && target != null) 
         {
             //Changes only the y position of the transform to match the target
             //Possibly replace this with a Mathf.Lerp for a better effect
             transform.position.y = target.transform.position.y;
         }
     }
    
     //On Collision Enter assigns the target once it is in range
     //Add any other platform initialization code here
     void OnCollisionEnter(collision col) {
         target = col.gameobject;
     }
 
     //On Collision Exit removes the target when out of range
     //Add any other platform deactivation code here
     void OnCollisionExit(collision col) {
         if (target == col.gameobject)
             target = null;
     }
 }
 
               If you need to move the object with physics replace transform.position.y = target.transform.position.y; with rigidbody.MovePosition() instead.
Nice! Thank you
This may be the way to go, in the future if Parenting won't work
Thanks!
~be
Your answer
 
             Follow this Question
Related Questions
Unity Object Aligment in Different Resolutions and Scales 0 Answers
One child object move differently as 0.0001f when moving parent 1 Answer
How to transform position of object A to object C (C is a child of B object) 1 Answer
Cardboard VR: Drag object towards player but have it stop just before 0 Answers
How to make throwable child object? 0 Answers