How to make a plataformer player move? (SOLVED)

I’m making a platform game mixed with shooter

I’m trying to make the character move, when I put its collision in Kinematic, he does what the script says, but it keeps floating

When, I put the collision in Dynamic, the character suffers the actions of gravity, but he not does what the script says

The script I wrote:
function init(self)
msg.post(".", “acquire_input_focus”)

        	self.moving = false
        	self.input = vmath.vector3()
        	self.dir = vmath.vector3(0, 1, 0)
        	self.speed = 1000
        end

        function update(self, dt)
        	if self.moving then
        		local pos = go.get_position()
        		pos = pos + self.dir * self.speed * dt
        		go.set_position(pos)
        	end

        	self.input.x = 0
        	self.input.y = 0

        	self.moving = false
        	if self.moving then
        		local pos = go.get_position()
        		pos = pos + self.dir * self.speed * dt
        		go.set_position(pos)
        	end
        end

        function on_input(self, action_id, action)
        	if action_id == hash('Up') then
        		self.input.y = 1
        		print("up") 
        	elseif action_id == hash("left") then
        		self.input.x = -1
        		print("left")
        	elseif action_id == hash("right") then
        		self.input.x = 1
        		print("right")
        	end

        	if vmath.length(self.input) > 0 then
        		self.moving = true 
        		self.dir = vmath.normalize(self.input)
        	end
        end

        function final(self)
        	msg.post(".", "release_input_focus")
        end

With Kinematic bodies you have to program all the movement yourself, including gravity.

How?

  • Have a variable to represent speed in the Y axis (let’s call it ysp)
  • On every frame, subtract a number from ysp. This number is the force of gravity, so play around with it until you find the amount you feel is right
  • Move the player in the Y axis by ysp * dt

In practice, it should look something like this:

function init(self)
    self.ysp = 0
end

function update(self, dt)
    self.ysp = self.ysp - 5
    local pos = go.get_position()
    go.set_position(pos + vmath.vector3(0, self.ysp, 0) * dt)
end

You also need to set ysp to 0 when the player touches the floor, but collision detection is a much bigger topic, so I didn’t cover it here.

1 Like

There is a platformer template project you can start from when creating a new project in the editor. The code is here:

1 Like

It worked!

1 Like

but, it is not colliding with the ground

Did you change anything?

Yes

So, I copied this code:


And it didn’t work, The character collides with the floor for a while and then falls

It’s because you forgot about this:

One way to know if the player touched the floor it is to check the Y component of the normal during collision handling. Something like:

-- This probably won't work, it's just to give an idea of how to do it
if message.normal.y == -1 then
    self.ysp = 0
end

I put this in my code:
function init(self)
self.correction = vmath.vector3()
end

function update(self, dt)
  self.correction = vmath.vector3()
end

function on_message(self, message_id, message, sender)
  if message_id == hash("contact_point_response") then
  if message.normal.y == -1 then
    self.ysp = 0
end
    if message.distance > 0 then
      local proj = vmath.project(self.correction, message.normal * message.distance)
      if proj < 1 then
        local comp = (message.distance - message.distance * proj) * message.normal
        go.set_position(go.get_position() + comp)
        self.correction = self.correction + comp
      end
    end
  end
end

Did I do it right?

But did you test the unmodified template project? I assume that worked?