Problem with RigidBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By csmith17979
:warning: Old Version Published before Godot 3 was released.

I working on trying to get some basic character movement going. I looked up a few tutorials and came up with this code:

extends RigidBody2D

export var speed = 600
export var jump = 10000
var ground = null

#button variables
var up = Input.is_action_pressed("up")
var left = Input.is_action_pressed("left")
var right = Input.is_action_pressed("right")

func _ready():
	set_fixed_process(true)
	ground = get_node("RayCast2D")
	ground.add_exception(self)

func _fixed_process(delta):
	up = Input.is_action_pressed("up")
	left = Input.is_action_pressed("left")
	right = Input.is_action_pressed("right")
	if up && ground.is_colliding():
		jump()
	elif left:
		 walk_l()
 	elif right:
		walk_r()
	else:
		idle()

func idle():
	set_axis_velocity(Vector2(0,0))

func walk_l():
	set_axis_velocity(Vector2(-speed,0))

func walk_r():
	set_axis_velocity(Vector2(speed,0))

func jump():
	set_axis_velocity(Vector2(0,-jump))

I have two problems:

  1. the speed and jump variables don’t seem to have any effect at all as to how the character moves, as long as there’s a number there, and the positive/negative is right (even if zero, so a -0 will jump, and a 0 doesn’t jump, a -100000000 and a -1 will jump the same height and speed). Changing the mass does seem to have an effect on how ti moves though.
  2. My character falls over whenever I move it.

My character so far is just a single sprite with a rectangle CollisionShape2D (scaled from the resource manager) and a raycast pointing down to check for the ground. Anyone have any guesses as to what I’ve done wrong?

:bust_in_silhouette: Reply From: Tybobobo

Re: 1:
Why do you have -jump inside jump()? Why the negative?
Perhaps you could try to use set_linear_velocity instead - try and see if that improves :slight_smile:

Re: 2. :
Check your RigidBody2D “Mode”;
enter image description here

Change it to “Character”

The negative is because computers write graphics upside down, if that makes sense. Changing the mode to character worked for fixing the falling down, thanks :slight_smile: Changing to linear_velocity doesn’t seem to help, but I can probably just work with mass if I can’t figure out what’s wrong.

EDIT: Never mind, was changing the value inside the code forgetting that it wasn’t changing in the export value. Thanks for the help!

csmith17979 | 2016-02-27 23:54