How can i make a Player that Jump Up and Falls Down
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How can i make a Player that Jump Up and Falls Down
How can i make a Player that Jumps Up and Falls Down? I Haven't Any Idea :/ the Key for Jumping must be Space :s
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: How can i make a Player that Jump Up and Falls Down
This tutorial might get you started in the right direction, without handing you the answer on a silver platter.
Kurosuke needs beta testers
Re: How can i make a Player that Jump Up and Falls Down
I'll break it down for you.
First you need a player that has the following, An X position, A Y position and a Y velocity. I'd suggest setting up a table called player and putting the stuff into it like such:
Then you would need a gravity variable. I usually set it to something around 800 to 1200. Next step would be making the player be affected by the gravity. The way that would work is quite simple. In love.update you would increase the players yVel by the gravity value each frame, Then set the player y position to whatever it is at the time + the yVel.
Something like this:
Now with that the player would just fall off the screen, then keep falling. I'm assuming you have some form of a ground, So you would want to write a function that checks if the player is on the ground or not. I'm not gonna get into that because the function would differ depending on how your game is set up. Main thing is, If the player is on the ground the function returns true, otherwise it returns false.
So now you'd have to check if the function returns true before the whole gravity thing above. Something like this:
Now you would have to stop the player somehow when he does touch the ground, So you add an else statement, and in it you set the yVel to 0 which stops the player. something like this:
Now you have the most basic form of physics. For the jumping all you do is set the yVel to some negative value, How high said value is depends on the strength of the gravity and how high you want the player to jump.
Something like this:
This would still be incomplete because the player can jump while in mid air, That can be fixed with a simple boolean. Hope this helps.
Note that this code is completely untested. There may be errors in it.
First you need a player that has the following, An X position, A Y position and a Y velocity. I'd suggest setting up a table called player and putting the stuff into it like such:
Code: Select all
player = {
x = 0,
y = 0,
yVel = 0
}
Something like this:
Code: Select all
function love.update(dt)
player.yVel = player.yVel + gravity * dt
player.y = player.y + player.yVel * dt
end
So now you'd have to check if the function returns true before the whole gravity thing above. Something like this:
Code: Select all
function love.update(dt)
if not onGround() then --if the onGround function returns false
player.yVel = player.yVel + gravity * dt
player.y = player.y + player.yVel * dt
end
end
Code: Select all
function love.update(dt)
if not onGround() then --if the onGround function returns false
player.yVel = player.yVel + gravity * dt
player.y = player.y + player.yVel * dt
else
player.yVel = 0
end
end
Something like this:
Code: Select all
function love.keypressed(key)
if key == " " then --Checks if the space key was pressed
player.yVel = -500
end
end
Note that this code is completely untested. There may be errors in it.
Re: How can i make a Player that Jump Up and Falls Down
Umm I Didn't Understand Anything :/
-
- Prole
- Posts: 2
- Joined: Mon Sep 22, 2014 1:15 am
Re: How can i make a Player that Jump Up and Falls Down
help!, it says that there is an error to perform on global 'gravity'(a nil value)
Re: How can i make a Player that Jump Up and Falls Down
You need to define the variable gravity.counterman2026 wrote:help!, it says that there is an error to perform on global 'gravity'(a nil value)
-
- Prole
- Posts: 2
- Joined: Mon Sep 22, 2014 1:15 am
Re: How can i make a Player that Jump Up and Falls Down
how do i do that?
would i do something like this?
gravity = {}
gravity = 10
would i do something like this?
gravity = {}
gravity = 10
Re: How can i make a Player that Jump Up and Falls Down
The simplest way would be to just put it into love.load.
Code: Select all
function love.load()
gravity = [[Whatever works for your game]]
end
Who is online
Users browsing this forum: Bing [Bot] and 1 guest