Reputation: 11
Godot Version 4.2.2
Question hello i am very very new to Godot so thank you for helping me. i am working on a 2d platformer and i am trying to add a dash to the game where the player moves towards the mouse and i got it working however when i dash diagonally it seems to go about half the distance as if i go straight up, down, left, or right, here is the script:
extends CharacterBody2D
@onready var sprite_0001_png = $"Sprite-0001_png"
const dash_time = 0.1
const dash_speed = 600
var dashing = false
var dash_charge = 1
var movement_lock = false
const SPEED = 250
const JUMP_VELOCITY = -600
var death = 0
var player_pos
var mouse_pos
func _process(delta):
mouse_pos = get_global_mouse_position()
player_pos = get_global_position()
pass
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func player_die():
velocity = Vector2(0, 0)
movement_lock = true
await get_tree().create_timer(1).timeout
dashing = true
get_tree().reload_current_scene()
movement_lock = false
func _physics_process(delta):
dash()
if not movement_lock == true and not is_on_floor():
velocity.y += gravity * delta
if is_on_floor() and dash_charge < 1:
dash_charge += 1
# Handle jump.
if not movement_lock == true and Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
if not movement_lock == true:
var direction = Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, 15)
var isright = velocity.x < 0
sprite_0001_png.flip_h= isright
move_and_slide()
func dash():
print(mouse_pos)
if dash_charge > 0 and not dashing:
var dir
if Input.is_action_just_pressed("dash"):
velocity = Vector2(0, 0)
movement_lock = true
dash_charge -= 1
dashing = true
dir = (mouse_pos - player_pos)
dir = dir.normalized()
velocity = dir * dash_speed
dash_charge -= 1
dashing = true
await get_tree().create_timer(dash_time).timeout
dashing = false
movement_lock = false
Upvotes: 1
Views: 236
Reputation: 1
Currently you have the dir variable normalized which makes the movement of the character a constant distance away so it acts as a radius of which you can dash. At least I think I don't fully know how normalized vectors work but in 3D a normalized vector has a 3D spere of possible directions it can point, all with a force of 1. What you would need to do is to remove the normalized part and set a maximum distance of the x and y that you want the player to move. After that it should be ok. If it doesn't IDK ¯\ (ツ) /¯
Upvotes: 0