![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Juanchoh |
Hey, so I’ve been scorching my brains here with this problem - I’ve got to say I’m quite new to this whole programing thing -, so I ask the community for their valious help. Ok, so I’m trying to replicate a Super Metroid type of shooting (press a button to aim in high diagonal, press another button for low diagonal, press both for aiming up), and I’ve been using vectors to try to solve this out but to no avail. Here is my code by parts:
Here are the states:
var is_pointing_high := Input.is_action_pressed("pointhigh")
var is_pointing_low := Input.is_action_pressed("pointlow")
var is_pointing_up := Input.is_action_pressed("pointhigh") and Input.is_action_pressed("pointlow")
var no_more_pointing := Input.is_action_just_released("pointhigh") or Input.is_action_just_released("pointlow")
Here are the conditions:
if is_pointing_high:
var diagup : Rect2 = Rect2(_palma.global_position, Vector2(1,-1))
var angup := Vector2(diagup.size.x, diagup.size.y) - global_position
rot = _palma.get_angle_to(angup)
if is_pointing_low:
var diagdown : Rect2 = Rect2(_palma.global_position, Vector2(1,1))
var angdown := Vector2(diagdown.size.x, diagdown.size.y) - global_position
rot = _palma.get_angle_to(angdown)
if is_pointing_up:
rot = -PI / 2
if no_more_pointing:
rot = 0.0
if is_shooting:
shoot()
The function for shooting:
func shoot() -> void:
var _bala := bala.instance()
_bala.start(_palma.global_position, sign(_pivot.scale.x), rot)
get_parent().add_child(_bala)
Aaaand the code in the bullet:
func start(pos: Vector2, dir, angle) -> void:
position = pos
_velocity = Vector2(speed * dir, 0).rotated(angle)
“rot” is my float variable for the angle, and “_palma” is a Position2D node on the palm of the hand of the player character.
Now, with this iteration of code, what ends up happening is the bullet presumably ends up hitting the character (thus destroying itself) while facing right and shooting in a low diagonal while facing left while pressing either button. If you press both buttons, the player will shoot down if facing right and will shoot up if facing left (the only thing I get right).
I appreciate your input on this matter. I will continue to work out this and tell you if I solve it on my own. Thanks!!