However, that is not satisfying enough for me. I want the rotation speed of the character to impact the output damage. Imagine a hammer or a flail, the harder you swing it the more deadly it is. My question is, how can I improve my code to get the results I want?
If you want to format your code you can use the standard triple backtick (```)
func hello():
print("Hello World!")
Or use the </> button at the top of the edit box. I suggest you always do that when pasting code as it makes the life of everyone much easier.
Regarding your issue I do not know how your current implementation looks. If you are manually rotating the object then you should already have all the information necessary to do a calculation based on that. If you are using physics simulations then look into properties like angular velocity and then do a calculation based on that.
Regarding implementation, I’m using a CharacterBody2D for the player character, and a look_at() function to point it towards the mouse. As far as I’m concerned, angular velocity is a property only reserved for rigid bodies and I can’t find anything else for character bodies.
I’ve been searching for this problem online before and it seems that this Reddit thread is the closest to the solution I’m looking for but there are problems with it too.
You can get the angular velocity with look_at() by keeping track of the rotation on the previous frame. Dividing the difference in rotation by delta gives you the angular velocity.
@onready var previous_rotation = rotation
func _physics_process(delta):
look_at(something)
var rotation_per_frame = fposmod(rotation - previous_rotation + PI, TAU) - PI
var angular_velocity = rotation_per_frame / delta
previous_rotation = rotation
If a weapon perfectly follows the mouse cursor, the angular velocity can get ridiculously high, so you might want to limit the maximum value for damage calculations.
If the damage should depend on the combined velocity of the character and its weapon, you need to calculate the velocity of the weapon and add it to the character’s velocity.