Impulsing to especific direction and problems with velocity

Godot Version

4.3

Question

i want to make a game about being a gun, so the charecter rotate to the mouse position. The problem is: when i shot, the idea is tha i’m thrown to the oposite direction, but idk how to calculate this vector

here’s my code:

extends CharacterBody2D

@onready var node_2d = $Node2D

var force = 10
var gravity = 570

func _physics_process(delta):

velocity.y = gravity

var mouse_position = get_global_mouse_position()
var dir = (mouse_position - position).normalized()
rotation = dir.angle()

if Input.is_action_just_pressed(“shoot”):
shoot(dir,mouse_position)

move_and_slide()

func shoot(direction: float,mouse_position):

velocity = velocity * direction

It would better if you use RigidBody for it.

You can get the opposite direction with just a minus sign in front of your direction.

var opposite_direction = -direction
2 Likes

Ok let me try

I’m gonna try it too

I think that using the rigid body could work better than the past two times (characterBody n physicsBody) and then i got the idea that for evite accumulation of impulse, before i add it i set the velocity to complete zero and then apply the impulse

Do you think it could work? (Guess there’s only one way to know)

EDIT: that did not worked

What do you mean it didn’t work? Was there an error? Did it look wrong? Behave wrong?
Please be specific.
And share your code using the preformatted text with ```

1 Like

it still accumulate impulse

extends RigidBody2D

@onready var node_2d = $Node2D
@onready var tile_map_layer = $“…/TileMapLayer”

var force = 7
var can_move = true

var linear_vel_x = clamp(0,0,200)
var linear_vel_y = clamp(0,0,200)

func _physics_process(delta):
print(get_linear_velocity())

var mouse_position = get_global_mouse_position()
var dir = ((mouse_position - position).normalized()).angle()
rotation = dir

if Input.is_action_just_pressed(“shoot”):
shoot(dir,mouse_position)

if !can_move:
linear_velocity = Vector2(0,0)

func shoot(direction: float, mouse_position):
var impulse: Vector2

impulse = (node_2d.global_position - mouse_position) * force

print(direction)
if can_move:
set_linear_velocity(Vector2(0,0))
apply_central_impulse(impulse)

this worked too

what if i limited my velocity?
(and how do i do that?)
@wchc

@KingGD
i tried limitating my velocity, but i think i’m missing something, because now my gravity is also limited AND it still accumulate velocity.

extends RigidBody2D

@onready var node_2d = $Node2D
@onready var tile_map_layer = $“…/TileMapLayer”

var force = 7
var can_move = true

var max_vel_x = 200
var max_vel_y = 200

func _physics_process(delta):
print(get_linear_velocity())

if get_linear_velocity().x > max_vel_x:
linear_velocity.x = max_vel_x
if get_linear_velocity().y > max_vel_y:
linear_velocity.y = max_vel_y

var mouse_position = get_global_mouse_position()
var dir = ((mouse_position - position).normalized()).angle()
rotation = dir

if Input.is_action_just_pressed(“shoot”):
shoot(dir,mouse_position)

if !can_move:
linear_velocity = Vector2(0,0)

func shoot(direction: float, mouse_position):
var impulse: Vector2

impulse = (node_2d.global_position - mouse_position) * force

print(direction)
if can_move:
apply_impulse(impulse,Vector2.ZERO)

func _on_area_2d_body_entered(body):
if body == tile_map_layer:
can_move = false

How you tried to limit the velocity?
And it would better if you write the codes in proper format like this:

``` I used this symbols for code format ```
2 Likes

Forget, it i was doing it for the pirate software game jam, and i was already thinking about quitting so I’m out, thanks for the both of you @KingGD and @wchc, if anyone’s reading this for getting help after make your on post.
Bye

2 Likes