S
SashoTAI
Guest
So I was trying to program the code for an enemy in a platformer game. I made it so if it touches the ground (obj_solid) it follows the player. So I added the gravity and the horizontal and vertical collision but it somehow still floats trough the ground. Any ideas? Here's my code:
Create event:
Create event:
- //Variables
- hspeed_ = 0;
- max_hspeed_= 10;
- vspeed_ = 0;
- gravity_ = 0.7;
- acceleration_ = 1;
- jump_height_ = -16;
- //Add physics
- if place_meeting (x + hspeed_, y, obj_solid)
- {
- while !place_meeting (x + sign(hspeed_), y, obj_solid)
- {
- x += sign(hspeed_);
- }
- hspeed_ = 0
- }
- x = x + hspeed_
- if place_meeting (x, y + vspeed_, obj_solid)
- {
- if instance_exists(obj_player)
- {
- move_towards_point(obj_player.x, obj_player.y, max_hspeed_)
- }
- while !place_meeting (x, y + sign(vspeed_), obj_solid)
- {
- y += sign(vspeed_);
- }
- vspeed_ = 0
- }
- y = y + vspeed_
- if hspeed_ != 0
- {
- hspeed_ += acceleration_;
- hspeed_ = clamp (hspeed_, -max_hspeed_, max_hspeed_)
- } else
- {
- hspeed_ = lerp (hspeed_, 0, .3)
- }
- if !place_meeting (x, y+1, obj_solid)
- {
- vspeed_ = vspeed_ + gravity_;
- }