Legacy GM Can you check my basic platform movement and collisions system?

J

Jejasu Desu

Guest
y-=vsp
vsp=0
if keyboard_check(vk_left)
{
if !place_meeting(x - 5, y, obj_wall) x -=5;
else x-=distance_to_object(obj_wall)
}

if keyboard_check(vk_right)
{
if !place_meeting(x + 5, y, obj_wall) x +=5;
else x+=distance_to_object(obj_wall)
}

if keyboard_check(vk_up)
{
vsp+=4
}

if(vsp>=16) or (key_released(vk_up))
{
vsp=0
}


if((vsp=0) and (!place_meeting(x,y+1,obj_ground)))
{
while(place_meeting(x,y+1,obj_ground))
{
y+=1
}
}

if(place_meeting(x,y-1,obj_wall))

{
vsp=0
}
 

jazzzar

Member
i don't understand what is the problem, i didn't read the code as i don't know where the problem is and you need to insert code next time
 

jo-thijs

Member
Hi and welcome to the GMC!

I'm sorry, but your code is terrible.

First of all, You don't use the W key anywhere in that code.
Unless you're using keyboard mapping, this would already explain why this code isn't making you jump when pressing W.

Secondly, you store the vertical speed of the player in the variable vsp,
but you reset that variable to 0 every step, causing any vertical motion to stop immediately after initiating.
This will cause any jumps to be very short (it will just make the player float 4 pixels above the ground).

Thirdly, we don't know what the script key_released looks like or what it does.
Judging by the name, I can guess, but I can't be sure.

And finally, you don't have any kind of working collision system for either horizontal or vertical movement.

I would suggest you'd search for some GameMaker platformer tutorials and toy around with stuff in GameMaker first.
 
A

Aura

Guest
Welcome to the GMC!

That's because you didn't tell it to jump upon pressing the W key? Your code is poorly structured and does not have a proper GML syntax. It does not make a logical sense either.

Drop that code and start from scratch by following a good tutorial. And read the Manual:

http://docs.yoyogames.com

...to learn what the proper syntax of using GML and various functions/variables is. (You can use the offline version by pressing F1 in the GameMaker IDE.)

In case you can't find a good tutorial or get confused about choosing the topics that have to looked into first, let me know and I shall explain how to code basic movement and collisions; but that'd be only after you show some initiative, don't copy-paste the code without learning how it works and do not throw TLDR cards at the Manual.
 

Imperial

Member
Create Event
Code:
hsp = 0;
vsp = 0;
grav = 1;
Jspd = 15 //Jump Height In Pixels
Step Event
Code:
Key_Right = keyboard_check(vk_right) || keyboard_check(ord("D"));
Key_Left = keyboard_check(vk_left) || keyboard_check(ord("A"));
Key_Jump = keyboard_check(vk_space) || keyboard_check(ord("W"))

if(place_meeting(x,y+1,ground)
{
    if(Key_Jump)
    {
        vsp -= Jspd;
    }
}
else
{
    if(vsp < 10)
    {
        vsp += grav;
    }
}

if(Key_Right)
{
    hsp = spd;
}
if(Key_Left)
{
    hsp = -spd;
}
if(Key_Right && Key_Left || !Key_Right && !Key_Left)
{
    hsp = 0;
}

if place_meeting(x+hsp,y,ground)
{
    while(!place_meeting(x+sign(hsp),y,ground))
    {
        x += sign(hsp);
    }
    hsp = 0;
}

x += hsp;

if place_meeting(x,y+vsp,ground)
{
    while(!place_meeting(x,y+sign(vsp),ground))
    {
        y += sign(vsp);
    }
    vsp = 0;
}

y += vsp;
 
Top