This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

So I've got a rigidbody2d, and when the player runs over it, it begins to follow the player.

using func integrateforces(state): I'm getting the correct direction, then applying an impulse in that direction * velocity.

Here's the code
While the player's actively moving, everything is smooth and looks great, but the issue is that when the player stops and the rigidbodies catch up to their intended target, of course, is that since I'm applying an impulse every time the function updates, it causes them to jitter when they shouldn't be moving.

I need these rigidbody2ds to follow the player, but also be affected by area2d gravity traps which will try to suck the rigid bodies in and destroy them, and I'm completely at a loss as to how I can have them smoothly follow the player, but still be affected by gravity. I'm open to alternatives as well.

I scripted something similar in unreal engine (that updates every tick) and it didn't jitter when stationary, so while I can get seemingly why it's jittering, I can't think of a viable alternative.

Again, I'd really appreciate any help, because I've been tearing my hair out over this.

in Engine by (48 points)
edited by

1 Answer

+2 votes
Best answer

Hey all,
found a solution to the issue I was having where rigidbody2ds would jitter when following the player (they would jitter around their intended destination).

I was initially doing a check to see if the absolute distance to the target location was over a certain amount in both the X and Y of the vector. If so, it would apply an impulse.

Here's what the jitter looked like:

Something like this:

func _integrate_forces(state):
    if updatingposition:
        if assignedasocket:
            followtarget = player.orbtargets[orbindex]
            var direction = (followtarget.global_position - global_position).normalized()
            if abs(direction.x) > .01 && abs(direction.y) > .01:
                apply_central_impulse(direction * 700)

This had the result of making the rigidbody jitter or bounce back and forth at distances equivalent to the .01 or whatever value I placed in the check.

Here it is with no jitter:

The correct solution was:

func _integrate_forces(state):
    if updatingposition:
        if assignedasocket:
            followtarget = player.orbtargets[orbindex]
            var direction = (followtarget.global_position - global_position).normalized()
            var distance_to_player = global_position.distance_to(followtarget.global_position)
            if distance_to_player > 10 :
                apply_central_impulse(direction * 700)    

Basically, I needed to check distance_to and play around until I found a happy medium distance wise. I'm sure there's probably a cleaner and easier way to do this, but if anyone else runs into a similar issue, this is what solved it for me.

by (48 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.