Skip to content

Seamless Path switch #141

Description

@Redsam121

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

Activity

  1. SALOway commented on Aug 1, 2023

    @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) 
    
  2. Redsam121 commented on Aug 3, 2023

    @Redsam121
    Author

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

  3. SALOway commented on Aug 3, 2023

    @SALOway

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions