Jump to content


Photo

Player Movement


  • Please log in to reply
8 replies to this topic

#1 ohaithar

ohaithar

    GMC Member

  • New Member
  • 150 posts
  • Version:GM8

Posted 17 April 2012 - 04:06 PM

Right now it's perfect, i don't get stuck or anything, but it's too slow. I don't know how to increase my speed without getting stuck in obj_solid. If i change my "y+=" to something like "y+=5" i get stuck in obj_solid when falling down.

Is this the best way to do it? Or is there an easier way, i just want a smooth movement on my character without being stuck all the time and all the other crazy stuff.

Btw, 'status' is just for my sprites.
if !place_meeting(x,y+1,obj_solid){
    y+=!place_meeting(x,y+1,obj_solid)
}
//LEFT
if (keyboard_check(vk_left) || keyboard_check(ord("A"))) && !place_meeting(x-1,y,obj_solid) {
    x-=!place_meeting(x-1,y,obj_solid)
    image_xscale=-1 
        if place_meeting(x-1,y,obj_solid) {
        hspeed = 0
        }
        if place_meeting(x,y+1,obj_solid) {
        status=1
        }        
        if !place_meeting(x,y+32,obj_solid){
        status = 2
        }
}
//RIGHT
if (keyboard_check(vk_right) || keyboard_check(ord("D"))) && !place_meeting(x+1,y,obj_solid) {
    x+=!place_meeting(x+1,y,obj_solid)
    image_xscale=1 
        if place_meeting(x+1,y,obj_solid){
        hspeed = 0
        }
        if place_meeting(x,y+1,obj_solid){
        status=1
        }        
        if !place_meeting(x,y+32,obj_solid){
        status = 2
        }
}
//RELEASE LEFT - RIGHT
if (keyboard_check_released(vk_left) || keyboard_check_released(vk_right)) or (keyboard_check_released(ord("A")) || keyboard_check_released(ord("D"))) /*&& status!=3*/ {
    hspeed=0 
    if place_meeting(x,y+1,obj_solid) {
    status=0
    }
}

Edited by ohaithar, 17 April 2012 - 04:08 PM.

  • 0

#2 jo-thijs

jo-thijs

    GMC Member

  • GMC Member
  • 1468 posts
  • Version:GM8.1

Posted 17 April 2012 - 06:42 PM

there are many different ways and none are good in all cases, but I'll give a way that is based on that your obj_solid actually is solid:
var(onground)=place_meeting(x,y,obj_solid);
// LEFT
if keyboard_check(vk_left)&&hspeed<=0{
if onground image_xscale=-1;
hspeed-=ACCELERATION;
}
// RIGHT
if keyboard_check(vk_right)&&hspeed>=0{
if onground image_xscale=1;
hspeed+=ACCELERATION;
}
// FRICTION
hspeed-=FRICTION*sign(hspeed);
if abs(hspeed)<FRICTION
hspeed=0;
// JUMP
if onground{
if keyboard_check(vk_up)
vspeed=-JUMPPOWER;
}else
// GRAVITY
vspeed+=WEIGHT;
// COLLISION
var(col_id)=instance_place(x+hspeed,y+vspeed,obj_solid);
if col_id{
move_contact_solid(direction,speed);
if x-sprite_xoffset+sprite_width<col_id.x-col_id.sprite_xoffset
|| x-sprite_xoffset>col_id.x-col_id.sprite_xoffset+col_id.sprite_width
hspeed=0;else
vspeed=0;
}
  • 0

#3 ohaithar

ohaithar

    GMC Member

  • New Member
  • 150 posts
  • Version:GM8

Posted 17 April 2012 - 07:17 PM

there are many different ways and none are good in all cases, but I'll give a way that is based on that your obj_solid actually is solid:

var(onground)=place_meeting(x,y,obj_solid);
// LEFT
if keyboard_check(vk_left)&&hspeed<=0{
if onground image_xscale=-1;
hspeed-=ACCELERATION;
}
// RIGHT
if keyboard_check(vk_right)&&hspeed>=0{
if onground image_xscale=1;
hspeed+=ACCELERATION;
}
// FRICTION
hspeed-=FRICTION*sign(hspeed);
if abs(hspeed)<FRICTION
hspeed=0;
// JUMP
if onground{
if keyboard_check(vk_up)
vspeed=-JUMPPOWER;
}else
// GRAVITY
vspeed+=WEIGHT;
// COLLISION
var(col_id)=instance_place(x+hspeed,y+vspeed,obj_solid);
if col_id{
move_contact_solid(direction,speed);
if x-sprite_xoffset+sprite_width<col_id.x-col_id.sprite_xoffset
|| x-sprite_xoffset>col_id.x-col_id.sprite_xoffset+col_id.sprite_width
hspeed=0;else
vspeed=0;
}

Just wanted to put this in code, because it's hard to read.
Gonna try this soon..
  • 0

#4 ohaithar

ohaithar

    GMC Member

  • New Member
  • 150 posts
  • Version:GM8

Posted 17 April 2012 - 09:36 PM

My character doesn't even move.. And i don't understand half of the code, at least the bottom part..

Some help?

EDIT : Well, it moves now, i had my friction too high. But still it's so weird, i don't need acceleration, maybe only jump acceleration. And my character usually just floats in obj_solid :unsure:

Edited by ohaithar, 17 April 2012 - 09:45 PM.

  • 0

#5 jo-thijs

jo-thijs

    GMC Member

  • GMC Member
  • 1468 posts
  • Version:GM8.1

Posted 18 April 2012 - 02:45 PM

Well, I can try giving a better code, but I'll need more information about your game and how you wnt him to move than.
  • 0

#6 ohaithar

ohaithar

    GMC Member

  • New Member
  • 150 posts
  • Version:GM8

Posted 19 April 2012 - 01:05 AM

Well, I can try giving a better code, but I'll need more information about your game and how you wnt him to move than.


It's a platformer.
I want him to move left and right, without acceleration. And jump, if you hold a key he jumps a little higher
  • 0

#7 decroded

decroded

    GMC Member

  • GMC Member
  • 524 posts
  • Version:GM8

Posted 19 April 2012 - 01:25 AM

While its great to do your own player movement code, it isn't always as easy as it first appears.
If you just want to cut out most of the hard work, try great script from brod.
Its really awesomely done and so easy to use for all sorts of objects in a platform game.
Once you get it plugged in, you can hack the script to handle variable jump heights etc.

Otherwise if you're still keen to do your own movement coding then go for it but just realise its going to slow down your overall progress on your project...

Edited by decroded, 19 April 2012 - 01:26 AM.

  • 0

#8 jo-thijs

jo-thijs

    GMC Member

  • GMC Member
  • 1468 posts
  • Version:GM8.1

Posted 19 April 2012 - 07:04 PM

Is this ok?
step event:

x-=(keyboard_check(vk_right)-keyboard_check(vk_left))*SPEED;
if place_meeting(x,y,obj_solid)
x=xprevious;
if place_meeting(x,y+1,obj_solid)&&keyboard_check_pressed(vk_up)
vspeed=-JUMPPOWER;
vspeed+=WHEIGHT;
if keyboard_check(vk_up)
vspeed-=HEIGHTCONTROL;
if place_meeting(x,y+vspeed,obj_solid){
y=yprevious;
while!place_meeting(x,y+sign(vspeed),obj_solid)
y+=sign(vspeed);
vspeed=0;
}
  • 0

#9 ohaithar

ohaithar

    GMC Member

  • New Member
  • 150 posts
  • Version:GM8

Posted 20 April 2012 - 06:11 PM

Ill try both suggestions tomorrow, thanks :thumbsup:
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users