0 votes

Hi everybody and sorry for my bad english i'm not too fluent and i'm new to godot too, i started doing some games in the engine and im liking it very much, i tried unity one time but i never could do anything on that engine until i found godot and some tutorials over the internet, today i can do some plataform games and rpg like pokemon and zelda, but none of then are what i really wanted to do, one of my projects was to make a racing game with police like NFS Carbon, i started drawing some cars and roads and i did make a demo, but my code to the car of the game it's too simple, it drives good but doesn't fell like a car, my code is this here:

extends KinematicBody2D

export (int) var speed = 150
export (float) var rotation_speed = 1.5

var velocity = Vector2()
var rotation_dir = 0

func get_input():
    rotation_dir = 0
    velocity = Vector2()
    if Input.is_action_pressed('ui_left'):
        rotation_dir -= 1
    elif Input.is_action_pressed('ui_right'):
        rotation_dir += 1
    if Input.is_action_pressed('ui_up'):
        velocity = Vector2(speed, 0).rotated(rotation)
    elif Input.is_action_pressed('ui_down'):
        velocity = Vector2(-speed, 0).rotated(rotation)

func _physics_process(delta):
    get_input()
    if velocity != Vector2():
        rotation += rotation_dir * rotation_speed * delta
    move_and_slide(velocity)

I dunno how to post my code better, but i would love if someone could help me it this code, everything that this code do its to move my car forward, backward and turn only if the car is in moviment like every car would do, but it doesn't accelerate like a car and don't have a extra force pushing my car left and right for drifting it, if someone could at least help me add some acceleration to my car, i would totally apreciate it. (and again sorry for the bad english...)

in Engine by (92 points)

1 Answer

0 votes
Best answer

I think your problem is the KineticBody2D, if you used the RigidBody2D there would already been some build-in function to control force (and therefore acceleration).

However i would prefer using a hand-made system for acceleration, you just need a little bit of mechanic knowledge, and any node will behave like a psychical object!
I would probably go with something like:

func _input(event):
   if event.is_action_pressed("up"):
       throttle=true
   elif event.is_action_released("up"):
      throttle=false 
#repeat the same 4 lines of code for break, turn_left, turn_right

func _process(delta):
    if turn_left:
       rotation+=rotation_value
       car_direction=rotate_car(rotation)        #dunno what´s the best function here, imagine a function that gives you the vector direction of the car
    elif turn_right:
       rotation-=rotation_value
       car_direction=rotate_car(rotation)
    if throttle: 
       speed+=increase_speed_value*car_direction
    elif break:
       speed-=decrease_speed_value*car_direction
    speed*=0.95 #friction
    position+=speed*delta
by (1,436 points)
selected by

Thanks, i'm going to test if works...

did it work?

i can't remember, this was so long ago i think i already made some other iteration of the code, but it's likely that the code worked, i'm working on other projects now, 3D ones, don't think i'll have time to work on this one again, it was a nice ideia, a nice concept too, but now i'm very busy with college and life, someday maybe, i like 2D projects too.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.