How to stop tweens from forcing through colliders?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LyguN

When the player is in range for the enemy to attack, the enemy launches to the player. This is done using a tween. The problem is that when I have multiple enemies, all attacking at the same time, they all go into eachother and then when the tween is finished they are pushed out of eachother. Is it possible to make it so that the tween still plays when the player is in attack-range, but it still detects collision and doesn’t force the enemy through each other?

You’re asking two systems (physics and tweens) to interact in some way; they’re going to conflict. Rather:

  • Consider checking for collisions before ‘tweening’ and avoid dashing (e.g. a raycast?).
  • Layer enemies so they don’t interact with each other.
  • Allow the tween to run in physics time; this may make the enemies behave different if you’ve physics based code.

The first probably makes more sense and will look more pleasing (e.g. the enemies appear smart) depending on your use case.

spaceyjase | 2021-12-10 16:52

The first two options aren’t viable for me because I still want the enemies to collide with eachother and make them able to attack at the same time. I should make it clearer what it is I wan’t to achieve: I wan’t it so that the tween plays the attack as normal except that it shouldn’t ignore the colliders I have set and force the attacking enemies through eachothers respective colliders. So basically, I want it so that if multiple enemies attack the player at the same time, then they should push eachother away, instead of moving inside eachother. I will try to film this and post it here.

As for:

Allow the tween to run in physics time

Im still a beginner and didn’t really understand this. Do you mean playing the tween in the _physics_process() function? Because Im already doing that.

LyguN | 2021-12-10 17:44

A tween has a TweenProcessMode property that can run in idle or the physics step. This may allow the colliders to respond although really, you should move the enemies without tweening to allow them to collide correctly. I’m unsure which properties you’re tweening but it’s likely just setting then so physics won’t ‘see’ the changes, if that makes sense.

spaceyjase | 2021-12-10 18:17

…and to clarify the raycast suggestion. You could apply a force to any colliding enemies so they are shunted out of the way along the direction’s tangent (+/-).

In fact, you could just apply an exploding force at some point along the attack vector (or each step of the tween) to push other enemies aside.

spaceyjase | 2021-12-10 18:21

Doesn’t work. The tween is basically unstoppable and will force it’s way through any collider or added force.

Also, here is the video demonstration.

As you can see, if multiple enemies attack at the same time they all go into eachother and when the tween finishes they get pushed out of eachother. This looks weird and as a result of this I also got that weird glitch at the end of the video.

LyguN | 2021-12-10 19:23