示例#1
0
        /// <summary>
        /// Create a new constraint given type and constraint frames.
        /// </summary>
        /// <param name="type">Constraint type.</param>
        /// <param name="referenceFrame">Reference frame.</param>
        /// <param name="connectedFrame">Connected frame.</param>
        /// <returns>Constraint component, added to a new game object - null if unsuccessful.</returns>
        public static Constraint Create(ConstraintType type,
                                        ConstraintFrame referenceFrame,
                                        ConstraintFrame connectedFrame)
        {
            GameObject constraintGameObject = new GameObject(Factory.CreateName("AGXUnity." + type));

            try {
                Constraint constraint = constraintGameObject.AddComponent <Constraint>();
                constraint.Type = type;

                constraint.AttachmentPair.ReferenceFrame = referenceFrame ?? new ConstraintFrame();
                constraint.AttachmentPair.ConnectedFrame = connectedFrame ?? new ConstraintFrame();

                // Creating a temporary native instance of the constraint, including a rigid body and frames.
                // Given this native instance we copy the default configuration.
                using (var tmpNative = new TemporaryNative(constraint.NativeType, constraint.AttachmentPair))
                    constraint.TryAddElementaryConstraints(tmpNative.Instance);

                return(constraint);
            }
            catch (System.Exception e) {
                Debug.LogException(e);
                DestroyImmediate(constraintGameObject);
                return(null);
            }
        }
示例#2
0
        /// <summary>
        /// Create constraint frame given parent, world position and world rotation.
        /// </summary>
        /// <param name="parent">Parent game object.</param>
        /// <param name="worldPosition">World position.</param>
        /// <param name="worldRotation">World rotation.</param>
        /// <returns>New constraint frame.</returns>
        public static ConstraintFrame CreateWorld(GameObject parent, Vector3 worldPosition, Quaternion worldRotation)
        {
            var frame = new ConstraintFrame(parent);

            frame.Position = worldPosition;
            frame.Rotation = worldRotation;
            return(frame);
        }