0 votes

I have a player RigidBody2D.I want to control it with keyboard like GTA2 character.I tried add_force() when press a key.It didnt work.It moves but when i press Left key it moves wrongfully.It should move forward when i press 'W' key,but when i press 'D' key it should turn slowly left.

My input function

func _input(event):
    if Input.is_action_pressed("ui_left"):
         add_force(Vector2(0,0),Vector2(-20, 0))
    elif Input.is_action_pressed("ui_right"): 
         add_force(Vector2(0,0),Vector2(20, 0))
    elif Input.is_action_pressed("ui_up"):
        add_force(Vector2(0,0),Vector2(0,20))
    elif Input.is_action_pressed("ui_down"): 
        add_force(Vector2(0,0),Vector2(0,-20))
    else:
        applied_force=Vector2(0,0)

I tried to change Mode of RigidBody2D to Kinematic.It doesnt move.The mode is Character.How i imlement GTA 2 style movement in Godot?

What I want
Thank you.

in Engine by (21 points)

2 Answers

+1 vote
Best answer

I solved by myself.I am sorry.Next time i will try too much to solve a problem.It seemed hard.I share the code if someone needs.

func _input(event):
    var vec=Vector2(0,0)
    linear_velocity=vec
    if Input.is_action_pressed("ui_left"):
        vec.x=-speed
    if Input.is_action_pressed("ui_right"): 
        vec.x=speed
    if Input.is_action_pressed("ui_up"):
        vec.y=speed
    if Input.is_action_pressed("ui_down"): 
        vec.y=-speed

    linear_velocity=Vector2(sin(ang)*vec.length(),cos(ang)*-vec.length())
by (21 points)

Just a heads up. If your angle is radians you can then rotate your vector2 using:

linear_velocity = vec.rotated(ang)

it is the same as calculating the sin and cos manually. http://docs.godotengine.org/en/3.0/classes/class_vector2.html

Thanks @MysteryGM.I didnt know.Simple code is always better

+1 vote

Character controllers rarely have actual physics, it would cause them to fall over when climbing etc.

You want a kinematic body, it allows animations etc with collisions but doesn't have all the physics parts to mess around with: http://docs.godotengine.org/en/3.0/tutorials/physics/using_kinematic_body_2d.html

by (1,482 points)
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 Frequently asked questions and 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 [email protected] with your username.