Set a constant static force to propel character in 2d side scroller?

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

Hi, I’m working on a sidescrolling 2d bit art skateboard game. I am brand new to making games, as well as coding. I finally got my movement controls figured out with a Kinematic Body 2D, and I have my level set up. But now I’m stuck again.

Previously I was using gravity on the x axis to pull my character to the right edge of the screen. But, since gravity increases velocity it won’t work for me. I need the force to remain constant.

When I first started I was using RigidBody, and I was able to create the right force through the physics forces in the inspector.

Thanks for any help or suggestions.

If i’m not mistaken, constant force = constant acceleration = variable velocity. What you want is constant velocity?

p7f | 2018-12-20 17:25

Correct, a constant velocity. My only concern is that I’m using velocity to control my character now, so I don’t want to interfere with the existing velocity. Ideally I’d like to employ another aspect of the physics engine so that the character’s overall motion remains the same.

extends KinematicBody2D

export (int) var speed = 300

var velocity = Vector2()

func get_input():
	velocity = Vector2()

if Input.is_action_pressed("ui_right"):
    velocity.x += 1
if Input.is_action_pressed("ui_left"):
    velocity.x -= 1
if Input.is_action_pressed("ui_down"):
    velocity.y += 1
if Input.is_action_pressed("ui_up"):
    velocity.y -= 1
velocity = velocity.normalized() * speed

func _physics_process(delta):
	get_input()
	move_and_slide(velocity)

I should have been more careful with my word choice. I don’t know what aspect of the physics engine I would need to employ, so I was using “force” to refer to the physics engine in general.

My character is instanced in a level scene, and i set a global gravity on the x axis, but it isn’t producing any results. I also tried creating an area 2d with x axis gravity on my level scene, but I couldn’t figure out how to get that to work either.

Do I need to insert code to activate gravity, or insert code to tell my character that gravity is affecting it? Any advice or suggestions are appreciated.

screen grab of project settings

ZachMoldof | 2018-12-21 02:16

:bust_in_silhouette: Reply From: p7f

Hi, KinematicBody2D does not get affected by gravity or other physics mechanics, as it’s supposed that you are controlling it programatically. For example, for a 2d Plataformer i would do:

var velocity = Vector2()
var gravity = 7000

func _physics_process(delta):
    #all my movement calculations here
    velocity.y += gravity*delta
    # move_and_slide and the resto of code

I guess you could doo the same for x axis and constant velocity like this:

var velocity = Vector2()
var final_velocity = Vector2()

var slow_down = -100 # or whatever velocity you want

func _physics_process(delta):
    #all my movement calculations here
    final_velocity.y = velocity.y
    final_velocity.x = velocity.x + slow_down
    #move_and_slide with final velocity

Something like that. Also, i would recommend you to run velocity = move_and_slide(velocity) so you have updated velocity afterwards in case you need it.