Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Aethenwulf |
i have a 2D character and walk freely with direction key’s, but i want to implement the controls now on touchscreen. i can’t find a tutorial on youtube that help me
i start from this to make my walk animation and coding
https://www.youtube.com/watch?v=Z9aR9IiiHT8
extends KinematicBody2D
const ACCELERATION = 500
const MAX_SPEED = 100
const FRICTION = 500
var velocity = Vector2.ZERO
onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get(“parameters/playback”)
func _physics_process(delta):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
animationTree.set("parameters/Idle/blend_position", input_vector)
animationTree.set("parameters/Run/blend_position", input_vector)
animationState.travel("Run")
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
else:
animationState.travel("Idle")
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
velocity = move_and_slide(velocity)
then i follow this tutorial to implement the touch screen controls https://www.youtube.com/watch?v=uGyEP2LUFPg
extends TouchScreenButton
var radius = Vector2(32, 32)
var boundary = 64
var ongoing_drag = -1
var return_accel = 20
var threshold = 10
func _process(delta):
if ongoing_drag == -1:
var pos_difference = (Vector2(0, 0) - radius) - position
position += pos_difference * return_accel * delta
func get_button_pos():
return position + radius
func _input(event):
if event is InputEventScreenDrag or (event is InputEventScreenTouch and event.is_pressed()):
var event_dist_from_centre = (event.position - get_parent().global_position).length()
if event_dist_from_centre <= boundary * global_scale.x or event.get_index() == ongoing_drag:
set_global_position(event.position - radius * global_scale)
if get_button_pos().length() > boundary:
set_position( get_button_pos().normalized() * boundary - radius)
ongoing_drag = event.get_index()
if event is InputEventScreenTouch and !event.is_pressed() and event.get_index() == ongoing_drag:
ongoing_drag = -1
func get_value():
if get_button_pos().length() > threshold:
return get_button_pos().normalized()
return Vector2(0, 0)
Do you honestly expect anyone to watch ~50 minutes of videos to help you with your issue? You didn’t even ask a question nor did you describe what exactly isn’t working. Post your code and people might be kind enough to take a look…
njamster | 2020-07-29 14:28