GTA 2 style character control

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By onurbulut

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.

I couldnt add image to question.Link:Connexion | Photobox

onurbulut | 2018-10-02 21:34

:bust_in_silhouette: Reply From: MysteryGM

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: Using KinematicBody2D — Godot Engine (3.0) documentation in English

:bust_in_silhouette: Reply From: onurbulut

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())

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. Vector2 — Godot Engine (3.0) documentation in English

MysteryGM | 2018-10-03 20:43

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

onurbulut | 2018-10-04 10:36