SebLague / Path-Creator

Path creation asset for Unity game development
https://assetstore.unity.com/packages/tools/utilities/b-zier-path-creator-136082
MIT License
1.8k stars 314 forks source link

Seamless Path switch #141

Open Redsam121 opened 1 year ago

Redsam121 commented 1 year ago

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

SALOway commented 1 year ago

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) 
Redsam121 commented 1 year ago

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

SALOway commented 1 year ago

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)