Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | HarryCourt | |
Old Version | Published before Godot 3 was released. |
I’m trying to make a Player (KinematicBody2D) jump when it lands on the Tileset, I can’t figure it out without crashing. The code below has a comment indicating where I think the error starts. Any ideas? Sorry for the small brief, I’m tired.
PlayerMovement.gd:
extends KinematicBody2D
const GRAVITY = 4000.0
#const WALK_SPEED = 2000
const JUMP_STRENGTH = 2500.0
const JUMP_LIMIT = 1.0
var jump_allowed_timer = 0
onready var sprite = get_tree().get_root().get_node("World/Player/CollisionShape2D/PlayerSprite")
func _ready():
set_fixed_process(true)
var velocity = Vector2()
export var PlayerSpeed = 1920
export var Smoothness = 0.2
func _fixed_process(delta):
velocity.y += delta * GRAVITY
if(jump_allowed_timer > 0):
jump_allowed_timer -= delta
var direction = 0
if (Input.is_action_pressed("ui_left")):
direction = -1
sprite.set_flip_h(true)
elif (Input.is_action_pressed("ui_right")):
direction = 1
sprite.set_flip_h(false)
velocity.x = lerp(velocity.x, PlayerSpeed * direction, Smoothness)
var motion = velocity * delta
motion = move(motion)
if (is_colliding()):
var normal = get_collision_normal()
motion = normal.slide(motion)
velocity = normal.slide(velocity)
move(motion)
# Problems (most likely) start here.
var collider = get_collider()
if (Input.is_action_pressed("ui_jump")) && is_jump_allowed():# && collider.is_in_group("Tileset"):
velocity.y -= JUMP_STRENGTH
jump_allowed_timer = JUMP_LIMIT
func is_jump_allowed():
return (jump_allowed_timer <= 0)
Is the indentation that way or got broken when you wrote it? because the get_collider
is outside the is_colliding
check.
If that is wrong, try to fix it so you can get better answers.
eons | 2017-10-01 15:16
No, it’s correct
HarryCourt | 2017-10-02 05:38