Godot Version
4.6.2
Question
Hello, I am newbie to GoDot and scripting in general, trying to learn through videos and trying to read documentation as best to my ability, and just experimenting with random scripts found through videos.
I was trying to make a vehicle in which I can enter and exit out. The problem I had was trying to
was with the Input.
If I use “ui_up” on the Input Action, I can enter and exit the car as freely as I can.
but when I use “interact”, I immediately enter, and exit the vehicle immediately.
I tried used print_debug, but I can’t still figure it out.
Here is the script for the car:
extends CharacterBody2D
@onready var car_sprite: AnimatedSprite2D = $CarSprite
@onready var car_collision: CollisionShape2D = $CarCollision
@onready var interactable: Area2D = $interactable
var speed = 200
var direction : Vector2
var active_player = false
func _ready() -> void:
interactable.interact = _on_car_interact
func _on_car_interact():
if active_player == false:
_enter_car()
func car_input():
if not active_player: return
direction = Input.get_vector("left","right","up","down")
velocity = direction * speed
move_and_slide()
#Problem (if "ui_up" is "interact"(E key), I enter and exit the vehicle immediately)
if Input.is_action_just_pressed("ui_up"):
_exit_car()
func _physics_process(delta: float) -> void:
car_input()
car_animation()
func car_animation():
if velocity.y < 0 && velocity.x < 0:
car_sprite.play("up_left")
car_collision.rotation_degrees = -45
interactable.rotation_degrees = -45
elif velocity.y < 0 && velocity.x > 0:
car_sprite.play("up_right")
car_collision.rotation_degrees = 45
interactable.rotation_degrees = 45
elif velocity.y > 0 && velocity.x < 0:
car_sprite.play("down_right")
car_collision.rotation_degrees = -135
interactable.rotation_degrees = -135
elif velocity.y > 0 && velocity.x > 0:
car_sprite.play("down_left")
car_collision.rotation_degrees = 135
interactable.rotation_degrees = 135
elif velocity.x < 0:
car_sprite.play("left")
car_collision.rotation_degrees = -90
interactable.rotation_degrees = -90
elif velocity.x > 0:
car_sprite.play("right")
car_collision.rotation_degrees = 90
interactable.rotation_degrees = 90
elif velocity.y < 0 :
car_sprite.play("up")
car_collision.rotation_degrees = 0
interactable.rotation_degrees = 0
elif velocity.y > 0:
car_sprite.play("down")
car_collision.rotation_degrees = 180
interactable.rotation_degrees = 180
func _enter_car():
var player = get_tree().get_first_node_in_group("player")
active_player = true
player.queue_free()
print_debug(active_player)
func _exit_car():
var player = preload("res://scenes/objects/player.tscn").instantiate()
active_player = false
get_tree().current_scene.add_child(player)
player.global_position = global_position
print_debug(active_player)