Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | GoodPie |
I’m having trouble with my current movement script for my player on a breakout-type game.
I have the desired movement worked out however, I’m not too sure how to prevent it from jumping when I lift my finger and touch the screen again in a different position. I’m using a KinematicBody2D
and modifying its position based on the current cursor position.
This is my current script:
extends KinematicBody2D
# How fast the player moves
export var moveSpeed = 2000;
# Track the position of the initial touch
var firstTouch = Vector2.ZERO;
# Whether we are using a touch screen of not
var usingTouch = false;
func _ready():
if (OS.get_name() in ["Android", "iOS"]):
usingTouch = true;
func _input(event):
if (Input.is_action_just_pressed("click")):
firstTouch = get_global_mouse_position();
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
if (usingTouch):
var currentMousePosition = get_global_mouse_position();
var movementDifference = currentMousePosition - firstTouch;
position = position.linear_interpolate(Vector2(currentMousePosition.x + movementDifference.x, position.y), 0.2);
I’ve tried several techniques and this is the closest I have gotten it to working the way I intend it besides the issue of it jumping on retouching.
Another method I have used is to detect drags within the _input
and applying move_and_collide
with the vector of the drag relative to the first touched position. I then added a boundary check with the last distance but this created a lot of “random” movement and wasn’t what I was after.
I’ve attached a screen recording demonstrating the issue. If someone could point me in the right direction, that would be greatly appreciated.