Exemplo n.º 1
0
        public static void AddLookAtConstraint(GameObject gobject, GameObject target, int index = -1)
        {
            LookAtConstraint constraint = gobject.GetComponent <LookAtConstraint>();

            if (null == constraint)
            {
                constraint = gobject.AddComponent <LookAtConstraint>();
                ParametersController parametersController = gobject.GetComponent <ParametersController>();
                if (null == parametersController)
                {
                    gobject.AddComponent <ParametersController>();
                }
                Constraint newConstraint = new Constraint {
                    gobject = gobject, target = target.transform, constraintType = ConstraintType.LookAt
                };
                if (index == -1)
                {
                    constraints.Add(newConstraint);
                }
                else
                {
                    constraints.Insert(index, newConstraint);
                }

                ParametersController targetParametersController = target.GetComponent <ParametersController>();
                if (null == targetParametersController)
                {
                    targetParametersController = target.AddComponent <ParametersController>();
                }
                targetParametersController.AddConstraintHolder(gobject);
            }
            else
            {
                // update visual target for LineRenderer
                foreach (Constraint c in constraints)
                {
                    if (c.gobject == gobject && c.constraintType == ConstraintType.LookAt)
                    {
                        c.target = target.transform;
                        break;
                    }
                }
            }
            ConstraintSource source;

            if (constraint.sourceCount == 0)
            {
                source = new ConstraintSource();
                constraint.AddSource(source);
            }
            else
            {
                source = constraint.GetSource(0);
            }

            source.sourceTransform = target.transform;
            source.weight          = 1f;
            constraint.SetSource(0, source);
            constraint.rotationOffset = new Vector3(0, 180, 0);

            constraint.constraintActive = true;

            GlobalState.FireObjectConstraint(gobject);
        }
Exemplo n.º 2
0
        public static void AddParentConstraint(GameObject gobject, GameObject target, int index = -1)
        {
            ParentConstraint constraint = gobject.GetComponent <ParentConstraint>();

            if (null == constraint)
            {
                constraint = gobject.AddComponent <ParentConstraint>();
                ParametersController parametersController = gobject.GetComponent <ParametersController>();
                if (null == parametersController)
                {
                    gobject.AddComponent <ParametersController>();
                }
                Constraint newConstraint = new Constraint {
                    gobject = gobject, target = target.transform, constraintType = ConstraintType.Parent
                };
                if (index == -1)
                {
                    constraints.Add(newConstraint);
                }
                else
                {
                    constraints.Insert(index, newConstraint);
                }
                ParametersController targetParametersController = target.GetComponent <ParametersController>();
                if (null == targetParametersController)
                {
                    targetParametersController = target.AddComponent <ParametersController>();
                }
                targetParametersController.AddConstraintHolder(gobject);
            }
            else
            {
                // update visual target for LineRenderer
                foreach (Constraint c in constraints)
                {
                    if (c.gobject == gobject && c.constraintType == ConstraintType.Parent)
                    {
                        c.target = target.transform;
                        break;
                    }
                }
            }

            ConstraintSource source;

            if (constraint.sourceCount == 0)
            {
                source = new ConstraintSource();
                constraint.AddSource(source);
            }
            else
            {
                source = constraint.GetSource(0);
            }
            source.sourceTransform = target.transform;
            source.weight          = 1f;
            constraint.SetSource(0, source);

            constraint.translationAtRest = gobject.transform.localPosition;
            constraint.rotationAtRest    = gobject.transform.localRotation.eulerAngles;

            Vector3 offset = Vector3.Scale(target.transform.InverseTransformPoint(gobject.transform.position), target.transform.lossyScale);

            constraint.SetTranslationOffset(0, offset);

            Quaternion quat = Quaternion.Inverse(target.transform.rotation) * gobject.transform.rotation;

            constraint.SetRotationOffset(0, quat.eulerAngles);

            constraint.constraintActive = true;

            GlobalState.FireObjectConstraint(gobject);
        }