Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Tato64 |
Hey guys, long story short what im trying to achieve is related to the player character dropping his weapon.
So far, right clicking while holding a weapon drops it at the character’s position, this is the related code:
var drop = ppistol.instance()
get_node("/root/World").add_child(drop)
drop.set_position(get_node("/root/World/Player").get_position())
Simple enough, right?
But i’d like the weapon to be “launched” a bit, according to where the player is looking, of course (Top Down 2D, Character looks at mouse position) and with some slight, random, angular force.
I’ve been reading up on apply_impulse, add_force, etc. but all my attempts have just ended up in crashes or not working at all.
Thanks in advance!
Hi! apply_central_impulse
after droping it and after adding it to the corresponding node should do the trick. Can you share what you tried so we can see why it crashed?
p7f | 2020-09-02 19:03
I deleted previous attempts, but for example, adding this line after the code i posted:
drop.apply_central_impulse(Vector2(100, 0))
Returns “Nonexistent function “apply_central_impulse” in base “Node2D””
Im assuming using “drop.”[…] should work, right? Since i use it on the previous line and it correctly places the drop on the player’s position
(Vector2 is 100x, 0y, since the player node faces positive X, and is always facing the mouse. 100 is just a testing value to see if this works)
Tato64 | 2020-09-02 19:32
Returns “Nonexistent function “applycentralimpulse” in base “Node2D””
This means your object is of class Node2D, instead of a RigidBody2D as you said in the question. Are you sure that ppistol
is the path to a RigidBody2D scene?
p7f | 2020-09-02 19:41
Oh crap, thanks.
ppsitol’s root was actually a Node2D. I made the rigidbody the new root and now apply_central_impulse
works!
But now there’s 2 issues:
-
It launches towards global positive X (So, to the right side of the screen)
-
It just keeps going and going
EDIT: A mixture of editing the linear damp of the pistol’s RigidBody2D and the force intensity of the apply_central_force
did the trick for issue n2. Im still trying to find out how to solve n1, im trying to turn the player angle into a Vector2 to see if it works.
Tato64 | 2020-09-02 19:51
For launching it on a particular direction, you just need to pass that direction to de apply_central_impulse
function… how you get that direction depends a lot from your current code. Do you want to launch it in the direction the player is looking? so you have to pass that direction, probably scaled by a constant.
Now, as you said the character looks to the mouse position, you can always take the direction like
var direction = character.global_position.direction_to(get_global_mouse_position())
Obviously, replace character
with your character node. And then use that direction
drop.apply_central_impulse(100*direction)
Change 100 for a value of your preference
p7f | 2020-09-02 23:23
Works like a charm! Thank you so much!
Tato64 | 2020-09-02 23:48
No problem! It would be nice if you add the resulting code as an anwser and select it, so others with the same question can find it easy. Or i can make the answer if you wish. So this question does not remain unanswered
p7f | 2020-09-02 23:53