void doUpdate() { if (!Spline || !Spline.IsInitialized) { return; } // Runtime processing if (Application.isPlaying) { int dir = Dir; // get the TF of the current distance. // Note: It's recommended to use the TF based methods in consecutive calls, as the distance based // methods need to convert distance to TF internally each time! float tf = Spline.DistanceToTF(mDistance); // Move using cached values(slightly faster) or interpolate position now (more exact) // Note that we pass mTF and mDir by reference. These values will be changed by the Move methods mTransform.position = (FastInterpolation) ? Spline.MoveByFast(ref tf, ref dir, Speed * Time.deltaTime, Clamping) : Spline.MoveBy(ref tf, ref dir, Speed * Time.deltaTime, Clamping); mDistance = Spline.TFToDistance(tf); // Rotate the transform to match the spline's orientation if (SetOrientation) { transform.rotation = Spline.GetOrientationFast(tf); } Dir = dir; } else // Editor processing: continuously place the transform to reflect property changes in the editor { InitPosAndRot(); } }
// Update is called once per frame void doUpdate() { if (!Spline || !Spline.IsInitialized) { return; } // Runtime processing if (Application.isPlaying) { int dir = Dir; // Move at a constant speed? if (MoveByWorldUnits) { // either used cached values(slightly faster) or interpolate position now (more exact) // Note that we pass mTF and mDir by reference. These values will be changed by the Move methods mTransform.position = (FastInterpolation) ? Spline.MoveByFast(ref mTF, ref dir, Speed * Time.deltaTime, Clamping) : // linear interpolate cached values Spline.MoveBy(ref mTF, ref dir, Speed * Time.deltaTime, Clamping); // interpolate now } else // Move at constant F // either used cached values(slightly faster) or interpolate position now (more exact) // Note that we pass mTF and mDir by reference. These values will be changed by the Move methods { mTransform.position = (FastInterpolation) ? Spline.MoveFast(ref mTF, ref dir, Speed * Time.deltaTime, Clamping) : // linear interpolate cached values Spline.Move(ref mTF, ref dir, Speed * Time.deltaTime, Clamping); // interpolate now } // Rotate the transform to match the spline's orientation if (SetOrientation) { transform.rotation = Spline.GetOrientationFast(mTF); } Dir = dir; } else // Editor processing: continuously place the transform to reflect property changes in the editor { InitPosAndRot(); } }
void Update() { Vector3 moveDelta = Vector3.zero; Vector3 oldPos = mTransform.position; // store old position float oldTF = TF; // store old tf float minY = mLastCurveY; // store old minimum height float moveaxis = Input.GetAxis("Horizontal"); bool jump = Input.GetButton("Jump"); // Handle Left/Right movement if (moveaxis != 0) { // Calculate new spline position, setting movement to x/z and storing minimum height int dir = (moveaxis > 0) ? -1 : 1; int newdir = dir; Vector3 newPos = Spline.MoveBy(ref TF, ref newdir, Mathf.Abs(moveaxis) * Speed * Time.smoothDeltaTime, CurvyClamping.Loop); // y-position needs extra handling, so just store x/z in moveDelta moveDelta.x = newPos.x - oldPos.x; moveDelta.z = newPos.z - oldPos.z; minY = newPos.y; } // Jumping (Y++) if (jump && mJumpDurationLeft > 0) { moveDelta += new Vector3(0, JumpSpeed * Time.smoothDeltaTime, 0); mJumpDurationLeft -= Time.deltaTime; } else // Gravity (Y--) { moveDelta += new Vector3(0, -Gravity * Time.smoothDeltaTime, 0); } // If we would move below the spline, restrict movement to stay above it if (oldPos.y + moveDelta.y < minY) { moveDelta.y = minY - oldPos.y; mJumpDurationLeft = JumpDuration; } // The actual moving if (moveDelta != Vector3.zero) { // Move and handle collision if (mController.Move(moveDelta) != CollisionFlags.None) { // If we're not on top of a collider => Halt and reset to last "valid" position if (mStopMoving) { mTransform.position = oldPos; TF = oldTF; minY = mLastCurveY; } } else { mStopMoving = false; } // Align rotation to spline mTransform.rotation = Spline.GetOrientationFast(TF); } mLastCurveY = minY; }
void Update() { if (!Spline || !Spline.IsInitialized) { return; } // *** Place Player in editor *** if (!Application.isPlaying) { if (Spline.Interpolate(TF) != mTransform.position) { mTransform.position = Spline.Interpolate(TF); } return; } int dir = 1; // advance on lane. We use PingPong clamping to detect when we reach the end of a spline (in that case dir changes!) Vector3 newPosOnSpline = Spline.MoveBy(ref TF, ref dir, Speed * Time.deltaTime, CurvyClamping.PingPong); Vector3 newTangent = Spline.GetTangent(TF); // Advance by spline curvation delta. We don't use newPosOnSpline directly because we don't want a snap-effect when in air or switching lanes mTransform.position += newPosOnSpline - mLastPosOnSpline; // *** Switch lanes? *** if (Input.GetButtonDown("Horizontal")) { if (TrySwitchLane(Input.GetAxis("Horizontal") < 0 ? Vector3.left : Vector3.right)) { newPosOnSpline = Spline.Interpolate(TF); } } // *** Jump? *** if (!mInAir && !Jumping && Input.GetKey(KeyCode.Space)) { StartCoroutine(Jump()); } // Set orientation mTransform.forward = newTangent; // Oops! We've reached end of spline. Check if we can fall down onto another spline... if (dir != 1) { CurvySplineBase newSpline; float newTF; if (RaycastForFollowUpSpline(Vector3.down, out newSpline, out newTF)) { // we found a new spline underneath us. Let's use it from now on Spline = newSpline; TF = newTF; newPosOnSpline = Spline.Interpolate(TF); mInAir = (newPosOnSpline - mTransform.position).sqrMagnitude > 0.001f; } else { StartCoroutine(Die()); // No Spline found. Time to die... } } // When in air or switching lanes, our position isn't where it should be. So we drag the player toward the lane position if (!Jumping) { Vector3 offset = mTransform.position - newPosOnSpline; if (offset.sqrMagnitude > 0.001f) { if (mInAir) { mTransform.position -= offset.normalized * Gravity * Time.deltaTime; } else { mTransform.position -= offset.normalized * SwitchSpeed * Time.deltaTime; } } else { mInAir = false; } } else // Perform a jump { mTransform.Translate(0, mJumpDelta * Time.deltaTime, 0, Space.Self); } mLastPosOnSpline = newPosOnSpline; }