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;
}
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'.
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.