private void UpdatePosition(float time) { pointsLength = spline.GetPoints().Length - 2; float pathSegments = spline.controlPoints.Length - 1; float segmentTime = 1 / pathSegments; timeStep = 1.0f / pointsLength; currentSegment = Mathf.CeilToInt(time / timeStep); currentPathSegment = currentSegment / resolution + 1; currentProgress = (time % timeStep / timeStep); currentSegmentDisplay = currentSegment; currentPathSegmentDisplay = currentPathSegment; Debug.Log(time / segmentTime - (currentPathSegment - 1)); if (currentSegment < pointsLength + 1) { Vector3 posStart = spline.GetPoints()[currentSegment].position; Vector3 tanStart = spline.GetPoints()[currentSegment].tangent; Vector3 posEnd = spline.GetPoints()[currentSegment + 1].position; Vector3 tanEnd = spline.GetPoints()[currentSegment + 1].tangent; transform.position = Vector3.Lerp(posStart, posEnd, currentProgress); transform.rotation = Quaternion.Slerp(Quaternion.LookRotation(tanStart), Quaternion.LookRotation(tanEnd), currentProgress); //if (tanStart.magnitude != 0 && tanEnd.magnitude != 0) { } }
public Vector3 GetPoint(int idx) { if (_isCatmull) { return(_catmull.GetPoints()[idx].position); } return(positions[idx]); }
public override void PaintTrajectory() { Vector3[] controlPoints = new Vector3[pointList.Count]; for (int i = 0; controlPoints.Length > i; i++) { controlPoints[i] = pointList[i].TransformVector.GetVector3(); } try { if (spline != null) { spline.Update(controlPoints); spline.Update(resolution, closedLoop); } else { spline = new CatmullRom(controlPoints, resolution, closedLoop); } splineVectors = spline.GetPoints(); trajectory.gameObject.GetComponent <LineRenderer>().positionCount = splineVectors.Length; trajectory.gameObject.GetComponent <LineRenderer>().SetPositions(splineVectors); } catch { print("Exeption PaintTrajectory()"); } }
// Start is called before the first frame update void Start() { _points = new Vector3[_gameObjects.Length]; for (int i = 0; i < _gameObjects.Length; i++) { _points[i] = _gameObjects[i].transform.position; } _catmull = new CatmullRom(_points, resolution, false); Debug.Log(_catmull.GetPoints().Length); }
private void UpdateSpline() { while (controlPoints.Count < controlPointCount) { frame.origin += GetFarthest(frame, farDetectionRange) * advanceFrameRatio; controlPoints.Enqueue(frame.origin); } spline.Update(controlPoints); splinePoints = spline.GetPoints(); }
private void UpdatePosition(float time) { pointsLength = spline.GetPoints().Length - 2; timeStep = 1.0f / pointsLength; currentSegment = Mathf.CeilToInt(time / timeStep); currentProgress = (time % timeStep / timeStep); currentSegmentDisplay = currentSegment; currentPathSegmentDisplay = currentSegment / resolution; if (currentSegment < pointsLength + 1) { Vector3 posStart = spline.GetPoints()[currentSegment].position; Vector3 tanStart = spline.GetPoints()[currentSegment].tangent; Vector3 posEnd = spline.GetPoints()[currentSegment + 1].position; Vector3 tanEnd = spline.GetPoints()[currentSegment + 1].tangent; Vector3 segmentPosition = CatmullRom.CalculatePosition(posStart, posEnd, tanStart, tanEnd, currentProgress); Vector3 segmentTangent = CatmullRom.CalculateTangent(posStart, posEnd, tanStart, tanEnd, currentProgress); transform.position = Vector3.Lerp(posStart, posEnd, currentProgress); transform.rotation = Quaternion.Slerp(Quaternion.LookRotation(tanStart), Quaternion.LookRotation(tanEnd), currentProgress); } }
void Update() { if (spline != null) { spline.Update(controlPoints); spline.Update(resolution); spline.DrawSpline(Color.white); if (drawNormal) { spline.DrawNormals(normalExtrusion, Color.red); } if (drawTangent) { spline.DrawTangents(tangentExtrusion, Color.cyan); } } else { spline = new CatmullRom(controlPoints, resolution); } if (speed > 0) { Debug.Log(spline.GetPoints().Length); float distance = Vector3.Distance(spline.GetPoints()[currentWaypointID].position, transform.position); transform.position = Vector3.MoveTowards(transform.position, spline.GetPoints()[currentWaypointID].position, Time.deltaTime * speed); var rotation = Quaternion.LookRotation(spline.GetPoints()[currentWaypointID].position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed); if (distance <= reachDistance) { currentWaypointID++; } } if (currentWaypointID >= spline.GetPoints().Length) { currentWaypointID = 0; transform.position = spline.GetPoints()[currentWaypointID].position; transform.rotation = Quaternion.LookRotation(spline.GetPoints()[currentWaypointID].position - transform.position); } }
// Update is called once per frame void Update() { if (isPlaying) { if (isOnConnector) { if (currentConnector.holdUntilTime > 0) { if (Input.GetButton(currentConnector.buttonName)) { chargeIndicator.gameObject.SetActive(true); holdTime += Time.deltaTime; } chargeIndicator.updateChargeBar(0, holdTime, currentConnector.holdUntilTime - currentConnector.startTime); float currentLerpTime = (gameTime - currentRail.startTime) / (currentRail.endTime - currentRail.startTime); playerObject.transform.position = Vector3.Lerp(currentRail.transform.position, currentRail.endPosition, currentLerpTime); if (Input.GetButtonUp(currentConnector.buttonName)) { if (Mathf.Abs(holdTime - (currentConnector.holdUntilTime - currentConnector.startTime)) < 0.2f) { releaseInTime = true; } calculateMovementCurve(currentConnector, currentConnector.pressed && releaseInTime); } if ((!releaseInTime && currentConnector.pressed && Input.GetButtonUp(currentConnector.buttonName)) || (releaseInTime && currentConnector.pressed) || (gameTime - currentConnector.startTime > 0.15f && !currentConnector.pressed)) { if (currentConnector.pressed && releaseInTime && currentConnector.pressedAction == ConnectorActionEnum.JUMP_TO_RAIL) { Debug.Log("releaseInTime"); currentLerpTime = (gameTime - currentConnector.holdUntilTime) / (currentConnector.endTime - currentConnector.holdUntilTime); //playerObject.transform.position = Vector3.Lerp(currentConnector.transform.position, currentConnector.pressedToRail.transform.position, currentLerpTime); playerObject.transform.position = LerpOverNumber(spline.GetPoints(), currentLerpTime); playerObject.GetComponent <Animator>().SetBool("isJumping", true); if (gameTime > currentConnector.endTime) { // finish connecting to other rail currentRail = currentConnector.pressedToRail; isOnConnector = false; isOnRail = true; releaseInTime = false; } } else if (currentConnector.unpressedAction == ConnectorActionEnum.JUMP_TO_RAIL) { Debug.Log("release missed"); float endTime = currentConnector.unpressedEndTime > 0 ? currentConnector.unpressedEndTime : currentConnector.endTime; currentLerpTime = (gameTime - currentConnector.startTime) / (endTime - currentConnector.startTime); //playerObject.transform.position = Vector3.Lerp(currentConnector.transform.position, Vector3.Lerp(currentConnector.unpressedToRail.transform.position, currentConnector.unpressedToRail.endPosition, (endTime - currentConnector.unpressedToRail.startTime) / (currentConnector.unpressedToRail.endTime - currentConnector.unpressedToRail.startTime)), currentLerpTime); playerObject.transform.position = LerpOverNumber(spline.GetPoints(), currentLerpTime); if (gameTime > endTime) { // finish connecting to other rail currentRail = currentConnector.unpressedToRail; isOnConnector = false; isOnRail = true; } } else { Debug.Log("release missed"); isOnConnector = false; isOnRail = true; } chargeIndicator.gameObject.SetActive(false); } } else { if (currentConnector.pressed && currentConnector.pressedAction == ConnectorActionEnum.JUMP_TO_RAIL) { float currentLerpTime = (gameTime - currentConnector.startTime) / (currentConnector.endTime - currentConnector.startTime); //playerObject.transform.position = Vector3.Lerp(currentConnector.transform.position, currentConnector.pressedToRail.transform.position, currentLerpTime); playerObject.transform.position = LerpOverNumber(spline.GetPoints(), currentLerpTime); playerObject.GetComponent <Animator>().SetBool("isJumping", true); if (gameTime > currentConnector.endTime) { // finish connecting to other rail currentRail = currentConnector.pressedToRail; isOnConnector = false; isOnRail = true; } chargeIndicator.gameObject.SetActive(false); } else if (currentConnector.unpressedAction == ConnectorActionEnum.JUMP_TO_RAIL) { chargeIndicator.gameObject.SetActive(false); float endTime = currentConnector.unpressedEndTime > 0 ? currentConnector.unpressedEndTime : currentConnector.endTime; float currentLerpTime = (gameTime - currentConnector.startTime) / (endTime - currentConnector.startTime); //playerObject.transform.position = Vector3.Lerp(currentConnector.transform.position, Vector3.Lerp(currentConnector.unpressedToRail.transform.position, currentConnector.unpressedToRail.endPosition, (endTime - currentConnector.unpressedToRail.startTime) / (currentConnector.unpressedToRail.endTime - currentConnector.unpressedToRail.startTime)), currentLerpTime); playerObject.transform.position = LerpOverNumber(spline.GetPoints(), currentLerpTime); if (gameTime > endTime) { // finish connecting to other rail currentRail = currentConnector.unpressedToRail; isOnConnector = false; isOnRail = true; } } else if (currentConnector.unpressedAction == ConnectorActionEnum.LEVEL_END) { isPlaying = false; playerObject.GetComponent <SpriteRenderer>().DOFade(0f, 1f); music.DOFade(0f, 1f); StartCoroutine(GameOver()); } else if (currentConnector.finished && currentConnector.unpressedAction == ConnectorActionEnum.NOTHING) { isOnConnector = false; isOnRail = true; chargeIndicator.gameObject.SetActive(false); } } playerObject.GetComponent <Animator>().SetBool("isNearConnector", false); } else if (isOnRail) { //playerObject.transform.Translate(Vector3.right * Time.deltaTime * rails[currentRail].movingSpeed); playerObject.GetComponent <Animator>().SetBool("isJumping", false); float currentLerpTime = (gameTime - currentRail.startTime) / (currentRail.endTime - currentRail.startTime); playerObject.transform.position = Vector3.Lerp(currentRail.transform.position, currentRail.endPosition, currentLerpTime); } if (gameTime > timeToLockCamera) { // if (cameraOffset == Vector3.zero) // { // cameraOffset = mainCamera.transform.position - playerObject.transform.position; // } // mainCamera.transform.position = playerObject.transform.position + cameraOffset; // if (mainCamera.transform.position.y < playerObject.transform.position.y - 2) { specificVector = new Vector3(playerObject.transform.position.x + 1, playerObject.transform.position.y - 2, mainCamera.transform.position.z); mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, specificVector, smoothSpeed * Time.deltaTime); } else if (mainCamera.transform.position.y > playerObject.transform.position.y + 2) { specificVector = new Vector3(playerObject.transform.position.x + 1, playerObject.transform.position.y + 2, mainCamera.transform.position.z); mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, specificVector, smoothSpeed * Time.deltaTime); } else { specificVector = new Vector3(playerObject.transform.position.x + 1, mainCamera.transform.position.y, mainCamera.transform.position.z); mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, specificVector, smoothSpeed * Time.deltaTime); } } // find area //foreach(AreaData areaData in areas) //{ // if (areaData.topCorner) //} if (playerObject.transform.position.y < -2 && !stillInWater) { fallTrigger = true; mixer.SetFloat("lowPassFreq", 2000); } else if (playerObject.transform.position.y > -2) { stillInWater = false; mixer.SetFloat("lowPassFreq", 20000); music.pitch = 1f; } if (fallTrigger) { stillInWater = true; fallTrigger = false; music.pitch = 0.5f; music.DOPitch(1f, 4f); } gameTime += (Time.deltaTime * music.pitch); if (Mathf.Abs(gameTime - music.time) > 0.02f) { music.time = gameTime; } } if (debugText) { string debugString = "gameTime: "; debugString += gameTime.ToString("0.0000"); debugString += " songTime: "; if (music) { debugString += music.time.ToString("0.0000") + " @ " + music.pitch.ToString("0.00"); float timeDiff = gameTime - music.time; float absTimeDiff = Mathf.Abs(timeDiff); debugString += "\ntimeDiff: "; debugString += timeDiff.ToString("0.0000"); debugString += "\n0.02: "; debugString += (absTimeDiff >= 0.02).ToString(); debugString += "\n0.01: "; debugString += (absTimeDiff >= 0.01).ToString(); debugString += "\n0.005: "; debugString += (absTimeDiff >= 0.005).ToString(); } debugString += "\nonRail: " + isOnRail.ToString() + " onConnector: " + isOnConnector.ToString(); debugText.text = debugString; } //if (Input.GetKeyDown(KeyCode.P) && !isPlaying) //{ // StartCoroutine(startPlaying()); //} if (Input.GetKeyDown(KeyCode.Escape)) { togglePause(); } }