I wanted to make a smooth move for a character but i dont really know how to make it out of the script that i got:
if Input.is_action_just_pressed("Stick"):
for dir in raycasts.keys():
if dir == "left":
is_sticking = false
reattach_timer = reattach_delay
velocity.x = left_force * speed
Now its “teleporting” instantly to a postition, but i want to make it move smootly and not to a desired position. Oh and with a limit to it. I hope you will understand now.
I tried 4 methods to do that but they didnt work for me. @gertkeno
I don’t think this code is relevant to teleporting then, it only changes velocity, not position. Could you paste more of your script (any part with position) or if you have Animations can you post how you set up those animations (especially if the modify position)
The only part using position is near the bottom, and it’s not a direct set.
As a nitpick you can remove the player declaration, for one this script is already the player so modifying position on it’s own modifies the player’s position. And if you want self you should use self instead of $".".
So a equivalent shortened line is as follows.
position += Vector2(0, -3)
However this shouldn’t telepor the player either! Is there anything else that modifies the player’s position? Can you share a video or two screenshots of what is happening? Can you describe what actions cause the teleport to happen? Can you describe the other 4 methods you tried?
I meant in this part of the script(this is the part that i need help with):
if Input.is_action_just_pressed("Stick"):
for dir in raycasts.keys():
var raycast = raycasts[dir]
if dir == "left" and raycast.is_colliding():
canDir = true
is_sticking = false
sprite.flip_h = false
reattach_timer = reattach_delay
velocity.x = -x_force * speed
Why x force multiplied by speed? This evaluates to 6,000 pixels per second, seems rather high considering the player normally moves at 200 pixels per second.
The part of the script is not culprit, but i want to modify it.
I want to modify it to move the player smoothly from the attached (sticked) position to a new position smoothly. Then when it start to move, the Player can stop the move with the release of the “Stick” action.
Still having trouble understanding. Maybe it would help to explain the bigger picture, is this a platformer? Is there anything like this in another game you could example?
Do you have the two “sticked” and “new position” figured out? Is one of them supposed to be the raycast variable?
Sounds like you want to be modifying velocity while the button is held, first try using Input.is_action_pressed as the function with just_pressed only applies to one frame when the button is initially pressed.
Sorry but i dont know really if there are any like it. So lemme explain it. I want the mechanics of the game to stick the player to walls when they hit it. Then add some enemies graphics and other stuff that platformers have. The mechanics will base as I said around the walls sticking, and lauching from them to beat the map, fight or get somewhere.
“Sticked” position is the “freezed” postion when one of the raycast is detecting and when the “Stick” action is pressed.
“New postion” will be the players postion + some pixels to the left or a Marker2D if that will be not a problem. @gertkeno
At least I’m sure you want to move the player from one position to another, you don’t have “Sticked”, “freezed”, or “New position” defined in your script so I can’t give a direct example.
Take your target position in global space, if it’s from the raycast’s raycast.get_collision_point() then it’s in global space. Then you can multiply the direction to that point by the speed you want the player to move.
var target: Vector2 = raycast.get_collision_point()
var direction: Vector2 = global_position.direction_to(target)
# apply the direction and speed
velocity = direction * speed
I’m betting you want to remove this velocity reset too, since this function is run every frame velocity = Vector2.ZERO will stop your stick movement instantly on the next frame.
Thank you for it but i have a problem. When the pull stops the player still moves to that direction that the force is applied to. Can you fix it in the script? @redflare
And after that you cant move.