Beispiel #1
0
        /// <summary>
        /// Physics computation of the joint
        /// </summary>
        void FixedUpdate()
        {
            if (connectedPath == null)
            {
                return;
            }

            // Re-align transform on path
            float minPos = computePosAtMinDistance();

            _tr.position = GetPositionForPos(minPos);

            //Debug.Log (GetPositionForPos (minPos));
            //Debug.DrawLine (_tr.position, GetPositionForPos (minPos), Color.cyan);

            if (followPathOrientation)
            {
                // Align rotation to the path
                Vector3    position = Vector3.zero;
                Quaternion rotation = Quaternion.identity;
                float      velocity = 1.0f;
                int        waypoint = 0;

                if (connectedPath.presampledPath)
                {
                    connectedPath.sampledPositionAndRotationAndVelocityAndWaypointAtPos(minPos, out position, out rotation, out velocity, out waypoint);
                }
                else
                {
                    rotation = connectedPath.computeRotationAtPos(minPos);
                }

                if (!connectedPath.disableOrientation)
                {
                    _tr.rotation = connectedPath.transform.rotation * rotation;
                }
            }

            // Computer direction vector
            Quaternion ffVq = connectedPath.GetFaceForwardForPos(minPos);
            Vector3    ffV  = ffVq * Vector3.forward;

            ffV = connectedPath.transform.TransformVector(ffV);

            // Constraint the velocity to the path direction
            _rb.velocity = Vector3.Dot(_rb.velocity, ffV) * ffV;

            //Debug.Log (minPos);

            // Apply motor force
            if (motor)
            {
                _rb.AddForce(ffV * motorForce);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Updates the target using stored currentPos.
        /// </summary>
        public void UpdateTarget()
        {
            Vector3    position = Vector3.zero;
            Quaternion rotation = Quaternion.identity;
            float      velocity = 1.0f;
            int        waypoint = 0;

            if (pathMagic.presampledPath)
            {
                pathMagic.sampledPositionAndRotationAndVelocityAndWaypointAtPos(currentPos, out position, out rotation, out velocity, out waypoint);
            }
            else
            {
                position = pathMagic.computePositionAtPos(currentPos);
                rotation = pathMagic.computeRotationAtPos(currentPos);
                velocity = pathMagic.computeVelocityAtPos(currentPos);
                waypoint = pathMagic.GetWaypointFromPos(currentPos);
            }

            if (globalFollowPath)
            {
                // Global follow path override
                rotation = pathMagic.GetFaceForwardForPos(currentPos);
            }
            else if (globalLookAt != null)
            {
                // Global look at override
                rotation = Quaternion.LookRotation(pathMagic.transform.InverseTransformPoint(globalLookAt.position) - position);
            }

            _lastVelocity = velocity;

            UpdateTarget(position, rotation);

            // Fire waypointChanged if is the case
            if (waypoint != _lastPassedWayponint)
            {
                if (waypointChanged != null)
                {
                    waypointChanged.Invoke(waypoint);
                }
                if (pathMagic.waypoints [waypoint].reached != null)
                {
                    pathMagic.waypoints [waypoint].reached.Invoke();
                }
            }

            _lastPassedWayponint = waypoint;
        }
        public void Generate()
        {
            if (count < 0)
            {
                count = 0;
            }

            List <GameObject> elements = new List <GameObject> ();

            for (int i = 0; i < count; i++)
            {
                if (i < transform.childCount)
                {
                    elements.Insert(i, transform.GetChild(i).gameObject);
                }
                else
                {
                    elements.Insert(i, Instantiate(target));
                    elements [i].transform.parent     = transform;
                    elements [i].transform.localScale = new Vector3(1, 1, 1);
                }
            }

            if (elements.Count < transform.childCount)
            {
                for (int i = transform.childCount - 1; i >= elements.Count; i--)
                {
                    DestroyImmediate(transform.GetChild(i).gameObject);
                }
            }

            for (int i = 0; i < elements.Count; i++)
            {
                float pos = startingFrom + (float)(1f / count * i) * (endTo - startingFrom);

                Vector3    position;
                Quaternion rotation;

                if (path.PresampledPath)
                {
                    float velocity;
                    int   waypoint;

                    path.sampledPositionAndRotationAndVelocityAndWaypointAtPos(
                        pos,
                        out position,
                        out rotation,
                        out velocity,
                        out waypoint);
                }
                else
                {
                    position = path.computePositionAtPos(pos);

                    rotation = path.computeRotationAtPos(pos);
                }

                elements [i].transform.position = path.transform.TransformPoint(
                    position
                    );

                elements [i].transform.rotation = transform.rotation * rotation;
            }
        }