Object Up and Down movement

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dang_

Hi im really confused about why this code won’t work properly. I used a similar code for side to side and it worked fine. I changed all the code that needed to be changed for it to move up and down but it still wont

extends KinematicBody2D

const LEFT = Vector2(-1, 0)
const GRAVITY = 0
const SPEED = 20

var motion = Vector2()
var up = Vector2(0, 1)
var down = Vector2(0, -1)
var direction = up
onready var animationPlayer = $AnimationPlayer

signal body_entered()
signal area_entered()

func _physics_process(delta):
	motion.x += GRAVITY
	motion.y = direction.y * SPEED
	motion = move_and_slide(LEFT, motion)
	animationPlayer.play("move")
	
	if is_on_wall():
		if direction == up:
			direction = down
		elif direction == down:
			direction = up
:bust_in_silhouette: Reply From: bellpepper

I’m not sure what you’re attempting to do exactly, but I think this issue is that the is_on_wall() conditional statement is being run every single physics frame.
This essentially just rapidly switches between the two up and down directions and potentially could be leaving the object unmoving.

It would be helpful to know what effect you’re trying to achieve though.

Well this character is supposed to move continuously and it has a wall barrier around it so that it goes back to other direction when it collides with the wall. The main problem is that it wont move at all. The animation plays and the body doesn’t move.

dang_ | 2020-10-22 09:50

I’m assuming that you want to move the character side to side. Instead of changing the up direction of the motion vector, you could instead have the linear velocity change directions instead.

Also, the return value of the move_and_slide function is a linear velocity vector upon a collision. So in this case, it returns Vector2(0, 0).
I’m not too knowledgeable on that though, but it’s something worth noting.

bellpepper | 2020-10-22 10:05

I would like this one to move up and down. I have another that works fine and its for side to side.

this is the original code for side to side

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 0
const SPEED = 15


var motion = Vector2()
var left = Vector2(-1, 0)
var right = Vector2(1, 0)
var direction = left
onready var animationPlayer = $AnimationPlayer

signal body_entered()
signal area_entered()

func _physics_process(delta):
	motion.y += GRAVITY
	motion.x = direction.x * SPEED
	motion = move_and_slide(motion, UP)
	animationPlayer.play("move")

	if is_on_wall():
		if direction == left:
			direction = right
		elif direction == right:
			direction = left

dang_ | 2020-10-22 10:24

Oh ok, I think I finally understand what you’re asking for now.

The up direction in the move_and_slide function is used to determine what is a floor and what is a ceiling. Think of it like a raycast. It does not move the object itself.
If you want the object to juggle between moving up and down, you need to change the linear velocity vector, which is your motion variable.

bellpepper | 2020-10-22 10:27

how would i change the velocity vector?

dang_ | 2020-10-22 10:29

Sorry this took me a minute. I had to quickly bring up a project to test this worked before I give you faulty code.

extends KinematicBody2D

const SPEED = 20

var motion = Vector2()
var up = Vector2(0, 1)
var down = Vector2(0, -1)
var direction = up
onready var animationPlayer = $AnimationPlayer

signal body_entered()
signal area_entered()

func _physics_process(delta):
	motion.y = direction.y * SPEED
	motion = move_and_slide(motion, Vector2.DOWN)
	animationPlayer.play("move")
	
	if is_on_ceiling():
		direction = down
	elif is_on_floor():
		direction = up

bellpepper | 2020-10-22 10:40

It worked thank you. Is there any way you could look through another script for me and try and help me. It is for a ballon and I want it to follow the mouse and be invisible. When u click on it it appears and you can drag and drop it anywhere. I really need help with this because it is a project and due tomorrow unfortunately.

extends KinematicBody2D

var dragging = false
var is_following_mouse = false
var mouse_position = get_global_mouse_position()
var grabbed_balloon = false
var move_to_mouse = true

onready var balloon_position

signal dragsignal;
signal body_entered()

export var move_speed = 650
export var stop_distance = 5


func _on_Balloon_body_entered(body):
	if body.get_name() == "Blue":
		body.queue_free()
	if body.get_name() == "Fire":
		body.queue_free()
		queue_free()
	if body.get_name() == "Fire_2":
		body.queue_free()
		queue_free()
	if body.get_name() == "Fire_3":
		body.queue_free()
		queue_free()
	
	
func _on_Balloon_ready():
	connect("dragsignal", self, "_set_drag_pc")
	$Sprite.hide()


func _on_Balloon_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed:
			$Sprite.show()
			$CollisionShape2D.set_deferred("disabled", false)
			emit_signal("dragsignal")
		elif event.button_index == BUTTON_LEFT and !event.pressed:
			$Sprite.show()
			$CollisionShape2D.set_deferred("disabled", false)
			emit_signal("dragsignal")
	elif event is InputEventScreenTouch:
		if event.pressed and event.get_index() == 0:
			self.position = event.get_position()
	elif event is InputEventMouseMotion:
		if balloon_position == mouse_position:
			emit_signal("followsignal")
		else:
			balloon_position == mouse_position
			emit_signal("followsignal")


func _process(delta):
	_move_to_mouse()
	if dragging:
		var mousepos = get_viewport().get_mouse_position()
		self.position = Vector2(mousepos.x, mousepos.y)


func _set_drag_pc():
	dragging=!dragging


func _move_to_mouse():
	if position.distance_to(get_global_mouse_position()) > stop_distance:
		var direction = get_global_mouse_position() - position
		var normalized_direction = direction.normalized()
		move_and_slide(normalized_direction * move_speed)
		$CollisionShape2D.set_deferred("disabled", true)

dang_ | 2020-10-22 10:59

Is there a reason for having the balloon be invisible and following the mouse until clicked on? Instantiating it at the mouse’s position could be much more easier.

bellpepper | 2020-10-22 11:10

Sorry its really messed up im sure.

dang_ | 2020-10-22 11:11

Im not sure what you mean but the balloon is supposed to follow the mouse so that when you click on the mouse it appears where you clicked. The main problem is i cant get rid of the ballon following the mouse meaning i cant get it to drop. Sorry if its hard to understand

dang_ | 2020-10-22 11:16

This is a bit inappropriate but since I understand you’re on a time limit, do you mind if we move this to a messaging application? The comments are going far off topic from the question and it would be much faster and easier to communicate to you elsewhere.

bellpepper | 2020-10-22 11:19

would email be easier??

dang_ | 2020-10-22 11:21

Do you have a Discord? If you do, you can share your tag and then hide the comment afterwards.

bellpepper | 2020-10-22 11:23

i dont unfortunately. I think it would be easier to just email because its not that slow

dang_ | 2020-10-22 11:24