-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Fix smooth follow example in interpolation #6134
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,24 +118,25 @@ Interpolation can be used to smooth movement, rotation, etc. Here is an example | |
|
||
func _physics_process(delta): | ||
var mouse_pos = get_local_mouse_position() | ||
|
||
$Sprite2D.position = $Sprite2D.position.lerp(mouse_pos, delta * FOLLOW_SPEED) | ||
var weight = 1 - exp(-FOLLOW_SPEED * delta) | ||
|
||
$Sprite2D.position = $Sprite2D.position.lerp(mouse_pos, weight) | ||
|
||
.. code-tab:: csharp | ||
|
||
private const float FollowSpeed = 4.0f; | ||
|
||
public override void _PhysicsProcess(float delta) | ||
{ | ||
Vector2 mousePos = GetLocalMousePosition(); | ||
|
||
Sprite2D sprite = GetNode<Sprite2D>("Sprite2D"); | ||
Vector2 mousePos = GetLocalMousePosition(); | ||
float weight = 1f - Mathf.Exp(-FollowSpeed * delta); | ||
|
||
sprite.Position = sprite.Position.Lerp(mousePos, delta * FollowSpeed); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
sprite.Position = sprite.Position.Lerp(mousePos, weight); | ||
} | ||
|
||
Here is how it looks: | ||
|
||
.. image:: img/interpolation_follow.gif | ||
|
||
This useful for smoothing camera movement, allies following you (ensuring they stay within a certain range), and many other common game patterns. | ||
This is useful for smoothing camera movement, allies following you (ensuring they stay within a certain range), and many other common game patterns. Notice that making movement framerate-independent is more involved than just multiplying weight by delta value. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The final notice not only doesn't bring much information, but may add confusion. I'd rather not add it.
AThousandShips marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest keeping the current version, but adding a comment right after :
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. Kind of kills the idea of fixing the incorrect method in the first place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the original method is not really wrong in itself. It just produces less smooth results, which might not be a problem in the context of a beginners tutorial.