public virtual void CreateSplineWalker( BezierSpline spline, SplineWalkerMode mode, int duration, bool canTriggerEvents = true, SplineWalkerTriggerDirection triggerDirection = SplineWalkerTriggerDirection.Forward, bool autoStart = true) { _Spline = spline; _AutoStart = autoStart; CanTriggerEvents = canTriggerEvents; TriggerDirection = triggerDirection; Duration = duration; WalkerMode = mode; SetPosition(0); GetProgress = 0; ResetTriggerIndex(false); _Spline.EventTriggered += EventTriggered; _oldKeyboardState = Keyboard.GetState(); _oldGamePadState = GamePad.GetState(PlayerIndex.One); Initialized = true; }
private void FollowPath(BezierSpline path) { spline = path; duration = spline.CalculateDuration(speed); isEscaping = true; t = 0; mode = SplineWalkerMode.Once; }
private void FixedUpdate() { if (!isAttacking) // nie atakuje { if (!isOnPath) { //Debug.Log ("Lece do sciezki " + spline.name); GetToPath(); } else { progress += Time.deltaTime / duration; if (mode == SplineWalkerMode.Loop) { if (progress > 1f) { progress -= 1f; } Vector3 position = spline.GetPoint(progress); GetComponent <Rigidbody> ().position = position; transform.LookAt(position + spline.GetDirection(progress)); //GetComponent<Rigidbody>().position += transform.forward * speed * Time.deltaTime; } else if (mode == SplineWalkerMode.Once) { if (progress > 1f) { //Debug.Log ("koniec ucieczki"); progress = 0f; // wczesniej 1f; //SetPath("patrol"); singlePlayerBoot1Controller.Patrol(); Destroy(currentEscapePath); isOnPath = false; isEscaping = false; spline = prevSpline; mode = SplineWalkerMode.Loop; } else { Vector3 position = spline.GetPoint(progress); GetComponent <Rigidbody> ().position = position; transform.LookAt(position + spline.GetDirection(progress)); //GetComponent<Rigidbody>().position += transform.forward * speed * Time.deltaTime; } } } } }
/// <summary> /// This is a coroutine for walking a spline section at constant speed in a defined time duration. /// </summary> /// <remarks> /// Based on the specified duration and the length of the specified spline section it calculates the required speed and then calls WalkAtSpeed. /// </remarks> /// <param name="startIndex">The start index of the spline section. Use start index greater than end index to invert the direction of motion.</param> /// <param name="endIndex">The end index of the spline section. Use end index lesser than start index to invert the direction of motion.</param> /// <param name="duration">The desired duration to cover the spline section.</param> /// <param name="transform">An optional transform object where to apply motion.</param> /// <param name="mode">The SplineWalkerMode: once, loop or ping pong.</param> /// <param name="lookForward">If set to <c>true</c> the transform rotation is set to curve direction.</param> /// <param name="completeFunction">This function will be called upon motion completion (only form SplineWalkerMode.Once).</param> /// <param name="updateFunction">This function will be called every frame.</param> /// <seealso cref="WalkAtSpeed"/> public IEnumerator WalkDuration( int startIndex, int endIndex, float duration, Transform transform, SplineWalkerMode mode = SplineWalkerMode.Once, Boolean lookForward = true, WalkCompleteFunction completeFunction = null, WalkUpdateFunction updateFunction = null) { _UpdateLengths(); // Calculate length from startPoint to endPoint int a = startIndex, b = endIndex; if (a > b) { int tmp = a; a = b; b = tmp; } float l = _arcLengths[b] - _arcLengths[a]; return(WalkAtSpeed(startIndex, endIndex, l / duration, transform, mode, lookForward, completeFunction, updateFunction)); }
public override void CreateSplineWalker(BezierSpline spline, SplineWalkerMode mode, int duration, bool canTriggerEvents = true, SplineWalkerTriggerDirection triggerDirection = SplineWalkerTriggerDirection.Forward, bool autoStart = true) { base.CreateSplineWalker(spline, mode, duration, canTriggerEvents, triggerDirection, autoStart); }
public void SetPath(string pathName) { switch (pathName) { case "death": spline = prevSpline; isOnPath = false; isAttacking = false; isEscaping = false; break; case "followPlayer": isAttacking = true; break; case "patrol": if (spline != patrolPath) { prevSpline = patrolPath; spline = patrolPath; duration = spline.CalculateDuration(speed); mode = SplineWalkerMode.Loop; progress = 0f; isOnPath = false; isAttacking = false; } break; case "captureBalloon1": if (spline != captureBalloon1Path) { prevSpline = captureBalloon1Path; spline = captureBalloon1Path; duration = spline.CalculateDuration(speed); mode = SplineWalkerMode.Loop; progress = 0f; isOnPath = false; isAttacking = false; } break; case "captureBalloon2": if (spline != captureBalloon2Path) { prevSpline = captureBalloon2Path; spline = captureBalloon2Path; duration = spline.CalculateDuration(speed); mode = SplineWalkerMode.Loop; progress = 0f; isOnPath = false; isAttacking = false; } break; case "captureBalloon3": if (spline != captureBalloon3Path) { prevSpline = captureBalloon3Path; spline = captureBalloon3Path; duration = spline.CalculateDuration(speed); mode = SplineWalkerMode.Loop; progress = 0f; isOnPath = false; isAttacking = false; } break; case "escape1": if (spline != korkociag && spline != petla && spline != beczka) { prevSpline = spline; currentEscapePath = (GameObject)Instantiate(korkociagGO, transform.position, transform.rotation); spline = currentEscapePath.GetComponent <BezierSpline>(); duration = spline.CalculateDuration(speed); mode = SplineWalkerMode.Once; progress = 0f; isEscaping = true; isOnPath = false; isAttacking = false; } break; case "escape2": if (spline != korkociag && spline != petla && spline != beczka) { prevSpline = spline; currentEscapePath = (GameObject)Instantiate(petlaGO, transform.position, transform.rotation); spline = currentEscapePath.GetComponent <BezierSpline>(); duration = spline.CalculateDuration(speed); mode = SplineWalkerMode.Once; progress = 0f; isEscaping = true; isOnPath = false; isAttacking = false; } break; case "escape3": if (spline != korkociag && spline != petla && spline != beczka) { prevSpline = spline; currentEscapePath = (GameObject)Instantiate(beczkaGO, transform.position, transform.rotation); spline = currentEscapePath.GetComponent <BezierSpline>(); duration = spline.CalculateDuration(speed); mode = SplineWalkerMode.Once; progress = 0f; isEscaping = true; isOnPath = false; isAttacking = false; } break; } }
/// <summary> /// This is a coroutine for walking a spline section at constant speed. /// </summary> /// <remarks> /// It uses the function GetProgressAtSpeed in order to update the curve parameter at each frame mantaining a constant speed. /// </remarks> /// <param name="startIndex">The start index of the spline section. Use start index greater than end index to invert the direction of motion.</param> /// <param name="endIndex">The end index of the spline section. Use end index lesser than start index to invert the direction of motion.</param> /// <param name="velocity">The desired velocity.</param> /// <param name="transform">An optional transform object where to apply motion.</param> /// <param name="mode">The SplineWalkerMode: once, loop or ping pong.</param> /// <param name="lookForward">If set to <c>true</c> the transform rotation is set to curve direction.</param> /// <param name="completeFunction">This function will be called upon motion completion (only form SplineWalkerMode.Once).</param> /// <param name="updateFunction">This function will be called every frame.</param> /// <seealso cref="WalkDuration"/> public IEnumerator WalkAtSpeed( int startIndex, int endIndex, float velocity, Transform transform = null, SplineWalkerMode mode = SplineWalkerMode.Once, Boolean lookForward = true, WalkCompleteFunction completeFunction = null, WalkUpdateFunction updateFunction = null) { float progress = startIndex / (float)curveCount; float limit = endIndex / (float)curveCount; yield return(GetPoint(progress)); int direction = endIndex > startIndex ? 1 : -1; while (true) { progress = GetProgressAtSpeed(progress, velocity, direction); if ((direction == 1 && progress >= limit) || (direction == -1 && progress <= limit)) { if (mode == SplineWalkerMode.Once) { break; } else if (mode == SplineWalkerMode.PingPong) { direction *= -1; if (direction * (endIndex - startIndex) > 0) { limit = endIndex / (float)curveCount; } else { limit = startIndex / (float)curveCount; } continue; } else if (mode == SplineWalkerMode.Loop) { progress -= limit - startIndex / (float)curveCount; continue; } } Vector3 position = GetPoint(progress); if (transform != null) { transform.position = position; if (lookForward) { transform.LookAt(transform.position + GetDirection(progress)); } } if (updateFunction != null) { updateFunction(position, progress); } yield return(null); } if (completeFunction != null) { completeFunction(); } }
public override void OnInspectorGUI() { walker = target as SplineWalker; // display all normal stuff first // assigned spline EditorGUI.BeginChangeCheck(); BezierSpline spline = (BezierSpline)EditorGUILayout.ObjectField(walker.spline, typeof(BezierSpline), true); //SplineWalker walker = (SplineWalker)EditorGUILayout.EnumPopup("spline mode: ", walker.mode) if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(walker, "Spline"); EditorUtility.SetDirty(walker); walker.spline = spline; } //looking foreward EditorGUI.BeginChangeCheck(); bool lookForeward = EditorGUILayout.Toggle("lookForeward", walker.lookForward); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(walker, "Toggle lookForeward"); EditorUtility.SetDirty(walker); walker.lookForward = lookForeward; } // fixed duration EditorGUI.BeginChangeCheck(); bool fixedDuration = EditorGUILayout.Toggle("FixedDuration", walker.fixedDuration); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(walker, "Toggle fixedDuration"); EditorUtility.SetDirty(walker); walker.fixedDuration = fixedDuration; } //movement mode EditorGUI.BeginChangeCheck(); SplineWalkerMode mode = (SplineWalkerMode)EditorGUILayout.EnumFlagsField("Mode", walker.mode); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(walker, "Mode"); EditorUtility.SetDirty(walker); walker.mode = mode; } //show the going foreward bool EditorGUI.BeginChangeCheck(); bool goingForeward = EditorGUILayout.Toggle("goingForeward", walker.goingForeward); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(walker, "Toggle goingForeward"); EditorUtility.SetDirty(walker); walker.goingForeward = goingForeward; } // does this walker have a fixed duration if (walker.fixedDuration) { // show the duration EditorGUI.BeginChangeCheck(); float duration = EditorGUILayout.FloatField("Duration", walker.duration); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(walker, "Duration"); EditorUtility.SetDirty(walker); walker.duration = duration; } } else { //show the velocity field EditorGUI.BeginChangeCheck(); float velocity = EditorGUILayout.FloatField("velocity", walker.velocity); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(walker, "velocity"); EditorUtility.SetDirty(walker); walker.velocity = velocity; } } // base.OnInspectorGUI(); }