public static AttachTransform Attach(GameObject target, Transform attachment, bool flipped, bool lockRotation, bool lockX = false, HeightLock lockY = HeightLock.Unlocked, bool lockZ = false) { AttachTransform at = target.GetComponent <AttachTransform>(); if (at == null) { at = target.AddComponent <AttachTransform>(); } // Note: Ideally child rot and child position should be passed in, and not recalculated by applying the inverses. It's unnecessary. at._attachment = attachment; at._startChildRotation = Quaternion.Inverse(attachment.rotation) * target.transform.rotation; at._startChildPosition = attachment.InverseTransformPoint(target.transform.position); at._startChildWorldPosition = target.transform.position; #if UNITY_EDITOR at._attached = true; #endif at._flipped = flipped; at._lockRotation = lockRotation; at._lockXOffset = lockX; at._lockYOffset = lockY; at._lockZOffset = lockZ; at.UpdateAttachment(); return(at); }
// TJ: Use this if we only want to lock the position of the thing as it moves, but not use the attach logic to position it public static AttachTransform LockPosition(GameObject target, bool lockX = false, HeightLock lockY = HeightLock.Unlocked, bool lockZ = false) { AttachTransform at = target.GetComponent <AttachTransform>(); if (at == null) { at = target.AddComponent <AttachTransform>(); } at._attachment = null; at._startChildWorldPosition = target.transform.position; at._lockXOffset = lockX; at._lockYOffset = lockY; at._lockZOffset = lockZ; return(at); }