Path-Creator icon indicating copy to clipboard operation
Path-Creator copied to clipboard

Seamless Path switch

Open Redsam121 opened this issue 2 years ago • 3 comments

I'm working on a rail shooter game using the path creator and contains alternate paths that can be taken.

The problem is that the transition of paths is not smooth, the player immediately snaps to next point. I want the player to smoothly fly to the newly entered path. How do I do that?

private void OnTriggerEnter(Collider other) { if (other.GetComponent<PathCreator>()) { if (pathcreator.gameObject == other.gameObject) return; pathcreator = other.GetComponent<PathCreator>(); distanceTravelled = pathcreator.path.GetClosestDistanceAlongPath(transform.parent.position); Debug.Log(pathcreator.path.GetClosestDistanceAlongPath(transform.parent.position)); } }

Capture

Capture2

Redsam121 avatar Jul 26 '23 05:07 Redsam121

If movement are controlled by setting the transform's position, then

// Value from 0 to 1. Determines how smoothly the movement will be performed. In this case, the smaller the value, the smoother it is
float smoothingFactor = 0.5f; 

// Find the nearest point along the path and get its distance from the initial point (If I understand it correctly)
travelledDistance = pathCreator.path.GetClosestDistanceAlongPath(transform.position); 

// Smoothly move from the current position to a point along the line
transform.position = Vector3.Lerp(currentPosition, pathcreator.path.GetPointAtDistance(travelledDistance), smoothingFactor) 

SALOway avatar Aug 01 '23 17:08 SALOway

If movement are controlled by setting the transform's position, then

// Value from 0 to 1. Determines how smoothly the movement will be performed. In this case, the smaller the value, the smoother it is
float smoothingFactor = 0.5f; 

// Find the nearest point along the path and get its distance from the initial point (If I understand it correctly)
travelledDistance = pathCreator.path.GetClosestDistanceAlongPath(transform.position); 

// Smoothly move from the current position to a point along the line
transform.position = Vector3.Lerp(currentPosition, pathcreator.path.GetPointAtDistance(travelledDistance), smoothingFactor) 

The lerp conflicts with main movement function called during Update(): void SetSpeed(float speed) { if (pathcreator) { speed /= 10; distanceTravelled += speed * Time.deltaTime; transform.parent.position = pathcreator.path.GetPointAtDistance(distanceTravelled); transform.parent.rotation = pathcreator.path.GetRotationAtDistance(distanceTravelled); currentSpeed = speed; } }

Redsam121 avatar Aug 03 '23 03:08 Redsam121

At the time of the trigger, you assign pathcreator and distanceTravelled a new value. Now, in Update, all you need to do is change the position via Lerp:

transform.parent.position = Vector3.Lerp(transform.parent.position, pathcreator.path.GetPointAtDistance(traveledDistance), movementSmoothingFactor)

You can also do the same thing for rotation

transform.parent.rotation = Quaternion.Lerp(transform.parent.rotation, pathcreator.path.GetRotationAtDistance(traveledDistance), rotationSmoothingFactor)

SALOway avatar Aug 03 '23 09:08 SALOway