/// <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); } }
/// <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; }