I'm working on a small game using lua with Löve2D, and I used this code to make my character jump:
epsilon = 0.1
x,y = player.body:getLinearVelocity()
if math.abs(math.ceil(y)) < epsilon then cantJump = false else cantJump = true end
if love.keyboard.isDown(" ") and not cantJump then player.body:setLinearVelocity(0,-500) end
player.body:setAngle(0)
player.body:setX(math.ceil(player.body:getX()))
player.body:setY(math.ceil(player.body:getY()))
end
But sometimes, I have to wait a short time to see the player jumping when I press space button. How can I fix that ?
I think the problem is the math.ceil. try removing it, since it is changing the player's position, or just move it to before you tell it to jump.
Related
The devforum was no help so I'm asking it here.
Im trying to make a Five Nights At Freddys game and I’m making the custom camera script, but its not facing the correct direction. Why is this happening?
Video:
https://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/0/2/4/024cca4de534dcfc084a944d3c2a13abf5382e0c.mp4
Code
RunService.RenderStepped:Connect(function()
if PlayerCharacter:WaitForChild("Player"):WaitForChild("isNightGuard").Value and not game.ReplicatedStorage:FindFirstChild("GameData"):FindFirstChild("inCams").Value then
Mouse.TargetFilter = game.Workspace.NightguardPosition
CurrentCamera.CFrame = CFrame.new(game.Workspace.NightguardPosition.CFrame.Position)
CurrentCamera.CFrame = CFrame.new(CurrentCamera.CFrame.Position, Mouse.UnitRay.Direction * 10)
end
end)
The NightguardPosition part is in the correct position and orientation.
I’ve tried many variations of the camera script but they all have the same result, please help?
The issue here is that for the CFrame.new(Vector3: position, Vector3: lookAt) constructor, the second Vector3 is what the CFrame will point at in world-space and is not a direction vector, unless position is (0, 0, 0), the origin. To fix the issue, you must add NightguardPosition.Position to the where the mouse is pointing in the world since that unit ray of the mouse like an offset when having to set lookAt.
Modified code with some comments (changes made to RenderStepped are suggestions and do not need to be built around!):
CurrentCamera.CameraSubject = workspace.NightguardPosition --// The CameraSubject is the LocalPlayer's Humanoid by default, causing some funky movements. Make sure to revert this
--// when cameras are exited.
--// Small performance improvement, unless this is changed elsewhere. Either way, it is best to only set when necessary and not in a looping behavior.
Mouse.TargetFilter = game.Workspace.NightguardPosition
local nightGuardPos = workspace.NightguardPosition.Position
RunService.RenderStepped:Connect(function()
--// CurrentCamera.CFrame = CFrame.new(game.Workspace.NightguardPosition.CFrame.Position)
--// Can be removed as this is the same as getting the Position of NightguardPosition
CurrentCamera.CFrame = CFrame.new(nightGuardPos, nightGuardPos + Mouse.UnitRay.Direction * 10)
end)
I might be stupid or something. But, I'm having a hard time with this. I usually find examples of code but it just confuses me. Unfortunately there isn't any good tutorial for this. I been using Lua for almost a year so I kinda have experience. Help would be extremely appreciate!
Basically I want to learn how to make rectangle jump up and then go back down.
For a single block that you control, you essentially need to store it's gravity and figure out a nice acceleration for it.
currentGravity = 0;
gravity = 1;
Then in the loop, you have to check if it's on ground using some collision detection. You want to add the gravity acceleration to currentGravity:
currentGravity = currentGravity + gravity
Then, you add it to the current y axis of the block:
YAxis = YAxis + currentGravity
Once you land, make sure to set gravity to 0. Also be sure to keep setting it to 0 to ensure you don't fall through the ground (as you kept adding to gravity no matter what.)
if not inAir() then
currentGravity = 0
end
And, of course, to jump, set currentGravity to a negative number (like 20) (if that's how you made the gravity work.)
Here's a collision detection function I made for Love2D:
function checkCollision(Pos1,Size1,Pos2,Size2)
local W1,H1,W2,H2 = Size1[1]/2,Size1[2]/2,Size2[1]/2,Size2[2]/2
local Center1,Center2 = {Pos1[1]+W1,Pos1[2]+H1},{Pos2[1]+W2,Pos2[2]+H2}
local c1 = Center1[1]+(W1) > Center2[1]-W2
local c2 = Center1[1]-(W1) < Center2[1]+W2
local c3 = Center1[2]+(H1) > Center2[2]-H2
local c4 = Center1[2]-(H1) < Center2[2]+H2
if (c1 and c2) and (c3 and c4) then
return true
end
return false
end
It assumes the position you gave it is the center of the block. If the box is rotated it won't work. You'll have to figure out how to make it work with walls and the like. And yes, it's ugly because it's very old. :p
okay so I have a jump button and a runtime listener function for when the jump button is pressed.
So whenver the jump button is pressed I apply linear impulse to the game hero.
local function controls(event)
if(event.phase=="began") then
if (buttonpressed.id=="jump") then
hero:applyLinearImpulse(0,-10,hero.x,hero.y)
end
elseif(event.phase=="ended") then
end
end
Now problem is if if the user keeps on tapping the jump button then the hero keeps on going up. I cant think of anyhting to counter this. One thing I could do is change the above code to:
local function controls(event)
if(event.phase=="began") then
if (buttonpressed.id=="jump" and hero.y>display.contentHeight/2) then
hero:applyLinearImpulse(0,-10,hero.x,hero.y)
end
elseif(event.phase=="ended") then
end
end
But this would still allow the jumping button to work until half of the screen is reached.
Kindly help me on this.
Thanks
Add a jump counter that resets when the player collides with the floor, that way you can allow double jumps etc later if you want to.
You could check the velocity of the hero and only allow the jump if the velocity is below a certain threshold:
if (buttonpressed.id=="jump" and hero.y>display.contentHeight/2) then
local vx, vy = hero:getLinearVelocity()
if vy < velocity_threshold then -- you have to define this value
hero:applyLinearImpulse(0,-10,hero.x,hero.y)
end
end
This should work. Another approach could be (i don't know much about corona) using linearDamping. This results in a damping of the linear motion, which sounds like something you could need.
You can also create a Boolean.
When the jump begins the boolean is true.
When the player collides with the floor is false.
While the boolean is true you cannot jump again.
In my endless runner game, I'm trying to remove an obstacle when it leaves the screen, then create a new one at a set of co-ords off to the right hand side of the screen. It works for the most part. The problem is, when I add a new obstacle, it momentarily flashes into existence at 0,0 (i.e. the bottom left of the scene...). Halp!
function updateObstacle()
if (obstacle) then
if(obstacle.x < -100) then
(obstacle):removeFromParent()
obstacle = nil
end
end
if (obstacle) then
(obstacle):translate(blockSpeed * -1, 0)
end
end
function newObstacle()
if (gameState == gameStates.gameStarted) then
if not (obstacle) then
createObstacle()
end
end
end
I ran into the same problem last year. Seems to be a bug, for details see
https://answers.madewithmarmalade.com/questions/34144/
This is the piece of code I am using to make the bird jump, but it's not even close to making a jump like flappy bird.it goes up on touch, ut does not fall like gravity is pulling it. Infact, after a couple of seconds, it looks like the bird is at its initial position as well as the jump position(two birds on screen), what can I do to fix this?
function Play: whenTouched()
self.touchStarted= true
x,y = self.myAnim:getPosition()
if self.touchStarted then
speed = -16
self.touchStarted = false
end
y = y + speed;
speed = speed + 2
self.myAnim:setPosition(x,y)
end
This is the equation you're looking for perhaps
http://www.school-for-champions.com/science/gravity_equations_upward_velocity.htm#.U60pj42SyDQ
Not sure how much math you have so If you need more help say so