Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seamless Path switch #141

Open
Redsam121 opened this issue Jul 26, 2023 · 3 comments
Open

Seamless Path switch #141

Redsam121 opened this issue Jul 26, 2023 · 3 comments

Comments

@Redsam121
Copy link

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
Copy link

SALOway commented Aug 1, 2023

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
Copy link
Author

Redsam121 commented Aug 3, 2023

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
Copy link

SALOway commented Aug 3, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants