Beispiel #1
0
    private void Start()
    {
        look = GetComponent <LookAtConstraint>();

        player = look.GetSource(0);
        target = look.GetSource(1);
    }
Beispiel #2
0
        bool HasParentOrConstraintSelected(Transform t, ref string parentLayerName)
        {
            Transform parent = t.parent;

            while (null != parent && parent.name != "RightHanded")
            {
                if (Selection.IsSelected(parent.gameObject))
                {
                    parentLayerName = LayerMask.LayerToName(parent.gameObject.layer);
                    return(true);
                }
                parent = parent.parent;
            }

            ParentConstraint constraint = t.gameObject.GetComponent <ParentConstraint>();

            if (null != constraint)
            {
                if (constraint.sourceCount > 0)
                {
                    GameObject sourceObject = constraint.GetSource(0).sourceTransform.gameObject;
                    if (Selection.IsSelected(sourceObject))
                    {
                        parentLayerName = LayerMask.LayerToName(sourceObject.layer);
                        return(true);
                    }
                }
            }

            LookAtConstraint lookAtConstraint = t.gameObject.GetComponent <LookAtConstraint>();

            if (null != lookAtConstraint)
            {
                if (lookAtConstraint.sourceCount > 0)
                {
                    ConstraintSource constraintSource = lookAtConstraint.GetSource(0);
                    GameObject       sourceObject     = constraintSource.sourceTransform.gameObject;
                    if (Selection.IsSelected(lookAtConstraint.GetSource(0).sourceTransform.gameObject))
                    {
                        parentLayerName = LayerMask.LayerToName(sourceObject.layer);
                        return(true);
                    }
                }
            }

            return(false);
        }
Beispiel #3
0
        void UpdateUIOnSelectionChanged(HashSet <GameObject> _, HashSet <GameObject> __)
        {
            if (null == selectedObjectNameLabel)
            {
                return;
            }
            // Clear
            selectedObjectNameLabel.Text = "";

            enableParentButton.Checked = false;
            parentTargetLabel.Text     = "";

            enableLookAtButton.Checked = false;
            lookAtTargetLabel.Text     = "";

            posLockButton.Checked   = false;
            rotLockButton.Checked   = false;
            scaleLockButton.Checked = false;

            GameObject selected = null;

            foreach (GameObject gobject in Selection.SelectedObjects)
            {
                selected = gobject;
                break;
            }

            if (null == selected)
            {
                return;
            }

            // Selected label
            if (Selection.SelectedObjects.Count > 1)
            {
                selectedObjectNameLabel.Text = $"{Selection.SelectedObjects.Count} objects selected";
            }
            else
            {
                selectedObjectNameLabel.Text = selected.name;
            }
            selectedObjectNameLabel.Text = "<color=#0079FF>" + selectedObjectNameLabel.Text + "</color>";

            // Transform
            UpdateTransformUI(selected);

            // Constraints
            ParentConstraint parentConstraint = selected.GetComponent <ParentConstraint>();

            if (null != parentConstraint && parentConstraint.sourceCount > 0)
            {
                enableParentButton.Checked = parentConstraint.constraintActive;
                parentTargetLabel.Text     = parentConstraint.GetSource(0).sourceTransform.name;
            }

            LookAtConstraint lookAtConstraint = selected.GetComponent <LookAtConstraint>();

            if (null != lookAtConstraint && lookAtConstraint.sourceCount > 0)
            {
                enableLookAtButton.Checked = lookAtConstraint.constraintActive;
                lookAtTargetLabel.Text     = lookAtConstraint.GetSource(0).sourceTransform.name;
            }
        }
Beispiel #4
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);
        }