Search code examples
touchgodottouch-eventgdscriptgodot3

I'm new to gdscript and I'm working on a ping pong game project, with touch as input type. I'm not able to Move the kinematicBody2D(paddle)


Here is the code:

extends KinematicBody2D

var is_dragging = false
var touchpos = 0


func _input(event):
    if event is InputEventMouseButton:
        if event.is_pressed():
            is_dragging = true
        else:
            is_dragging = false
    if is_dragging:
        touchpos = event.position

func _physics_process(_delta):
    if is_dragging:
        touchpos.x = 70
          $Sprite.global_position = touchpos

I was expecting the Ball to bounce on the paddle but it was passing through it as only Sprite was moving. Not the kinematicBody2D. So, can anyone tell me how to do that?


Solution

  • You basically answered it yourself. Your code only changes the position of your sprite, not the position of your kinematicbody.

    Change:

    $Sprite.global_position = touchpos
    

    to

    global_position = touchpos
    

    This will move rhe whole body with all their children.