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.
+1 vote

My scene tree looks like:

Character (RigidBody2D)
 -> CollisionShape2D
 -> MeleeHitbox (Area2D)
    -> CollisionShape2D

In my main scene's "_process" function, I call character.linear_velocity = <non-zero Vector2> to make my characters move. When the MeleeHitbox is added to my Character scene, the characters will not move. When I remove it, they move perfectly as I'd expect.

Any ideas what's going on here?

I've tried disabling all collision layers/masks on the MeleeHitbox. I've tried moving the MeleeHitbox CollisionShape2D so it doesn't overlap my RigidBody2D's CollisionShape2D. I've tried:

func _integrate_forces(state):
  state.set_linear_velocity(cur_velocity)

on my RigidBody2D instead of using self.linear_velocity. I've tried using apply_central_impulse() instead of self.linear_velocity. I've tried setting the linear_damp field on the MeleeHitbox to 0. None of these experiments had any effect; the only thing that works is if I remove the MeleeHitbox entirely.

Godot version 3.3.2
in Engine by (22 points)

It's not possible to reproduce your situation and get the behavior that you're getting by following exactly whatever you said you did. You clearly need to share more details with us so we can reproduce the situation with the minimum amount of steps. Can you recreate the objects and scripts from scratch and check at which exact point the problem occurs?

I'd like to ask you something:
Why are you controlling the velocity of a RigidBody2D? It's explicitly stated in the documentation that one shouldn't alter the position of linear velocity at every frame. Stick with applying a force or impulse instead.

I'll work on a minimal example later today. WRT the linear velocity question; I agree that I'm abusing the system a bit here. It has been working for me so far. I'd like to transition to using the _integrate_forces system as I mentioned in my answer (where I set cur_velocity in my code then make the linear_velocity change in _integrate_forces). Hopefully that will be a good long term solution. Unless there's a better practice to do this? My end goal is to have objects that react to impacts (getting hit by other objects), while still having their velocity being file-tunable so I can have them path around a math (e.g. with astar).

An update: it seems like this problem is caused by my usage of character.rotation = <angle> in the line of code either before or after my character.linear_velocity = <vector> usage. If I remove the rotation call, everything works as expected. Digging into why this might be...

I'd like to ask you something:
Why are you controlling the velocity of a RigidBody2D? It's explicitly stated in the documentation that one shouldn't alter the position of linear velocity at every frame. Stick with applying a force or impulse instead.

2 Answers

+1 vote
Best answer

I fixed this issue by not setting character.rotation = <angle> in my _process code, instead using:

  character.angular_velocity = 40 * sin((direction.angle() - character.rotation) / 10)

to smoothly have the character turn until my desired direction is equal to my character's rotation.

Probably next I will mess around more with _integrate_forces, since that seems like the proper way to mess with linear/angular velocities.

by (22 points)
0 votes

Did you try using

void add_force(offset: Vector2, force: Vector2)

#Adds a positioned force to the body. 
#Both the force and the offset from the body origin are in global coordinates.
by (810 points)

He said that he tried applying an impulse. I don't think applying a force would work when an impulse doesn't.

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.