Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | godotboy | |
Old Version | Published before Godot 3 was released. |
My character is kinematic body 2D and the object is area2D. My character has to accelerate up when it collides with the object. What’s a way to approach this? I’ve been stuck on this for hours and can’t find an answer.
If you have an acceleration
variable, you can just give it a negative y
value on collision.
How are you doing the movement? If you post your movement code, it might help to point you in the right direction.
kidscancode | 2017-09-23 01:31
Hi, thank you for responding. Acceleration variable has negative y value indeed. I tried using signal body.enter to resolve the issue, but it didn’t work. What do you see in my code that’s wrong?
extends KinematicBody2D
onready var ground_ray = get_node("ground_ray")
onready var bell = preload("res://bell.tscn")
const ACCEL = 1500
const MAX_SPEED = 500
const FRICTION = -500
const GRAVITY = 4000
const JUMP_SPEED = -1400
const MIN_JUMP = -500
var acc = Vector2()
var vel = Vector2()
func _ready():
var b = bell.instance()
b.connect("bell_out", self, "_on_bell_out")
set_fixed_process(true)
set_process_input(true)
func _input(event):
if event.is_action_pressed("ui_up") and ground_ray.is_colliding():
vel.y = JUMP_SPEED
func _on_bell_out():
vel.y= JUMP_SPEED
move(vel)
func _fixed_process(delta):
acc.y = GRAVITY
acc.x = Input.is_action_pressed("ui_right") - Input.is_action_pressed("ui_left")
acc.x *= ACCEL
if acc.x == 0:
acc.x = vel.x * FRICTION * delta
vel += acc * delta
vel.x = clamp(vel.x, -MAX_SPEED, MAX_SPEED)
var motion = move(vel * delta)
if is_colliding():
var n = get_collision_normal()
motion = n.slide(motion)
vel = n.slide(vel)
move(motion)
godotboy | 2017-09-23 04:26