How to make my character flying towards mouse with constant speed?

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

I wrote this part of character script(in _physics_process):


	var dpos = (get_global_mouse_position()-global_position).normalized()
	set_axis_velocity(dpos*1024)

so, it dont works correctly. Is there any way to make it work better without rotating character?

:bust_in_silhouette: Reply From: kidscancode

You’re getting the direction vector towards the mouse, but you need to apply that to the body via adding force.

In general, RigidBody2D shouldn’t have its position or velocity set directly, because it’s subject to the physics engine’s calculations.

See the RigidBody2D Reference for more. There’s a lot of good info there.

So you can either use set_applied_force() to “push” the body towards the mouse, or use _integrate_forces() instead of _physics_process() where you can access the direct body state to set the linear_velocity.

Here’s a more detailed writeup I did on rigid bodies, showing several examples:
http://kidscancode.org/blog/2017/12/godot3_kyn_rigidbody1/

Not helped. It still have problems with controlling direction. What can i do?

P.S. my english is not very good

CaveJohnson376 | 2018-05-04 13:28

You’ll have to be more specific. It’s hard to guess what’s wrong when you say “have problems”. Post your code and show what you’re doing.

kidscancode | 2018-05-04 13:39