V
VicoZann
Guest
As I was adding up more mechanics to my game I noticed that, for some reason, whenever my character is in his defense state script and moving, the camera stops in place, going back to following him when he stops moving or goes to another state.
This is the creation code for the camera
And this the step event for the camera
I'm failling to notice what's making the camera stop working when changing to that specific state and only when moving in it.
Finally this is the script for the defense state. The camera object is just an empty object that moves around where it's needed, no code in there. Everything is in the create and step events of the player character
///create camera
instance_create(x,y,obj_cam); // create the camera object at the player's location
cam = obj_cam;
view_hborder = view_wview/2; //set the view's horizontal border to keep the camera object centered
view_vborder = view_hview/2; // set the view's vertical border to keep the camera object centered
// Initialize Camera Variables
h_dist = 50; // set how far away horizontally the camera should be from the player
v_dist = 50; // set how far away vertically the camera should be from the player
cam.xTo = x + h_dist; // set the cam's initial xTo position
cam.yTo = y - v_dist; // set the cam's initial yTo position
peek = sprite_width + 1; // set the distance to check if a player is looking over a ledge
instance_create(x,y,obj_cam); // create the camera object at the player's location
cam = obj_cam;
view_hborder = view_wview/2; //set the view's horizontal border to keep the camera object centered
view_vborder = view_hview/2; // set the view's vertical border to keep the camera object centered
// Initialize Camera Variables
h_dist = 50; // set how far away horizontally the camera should be from the player
v_dist = 50; // set how far away vertically the camera should be from the player
cam.xTo = x + h_dist; // set the cam's initial xTo position
cam.yTo = y - v_dist; // set the cam's initial yTo position
peek = sprite_width + 1; // set the distance to check if a player is looking over a ledge
This is the creation code for the camera
///Move Camera
if(input_sprint && state != PLAYER_STATE.defense)
{
h_dist = 130;
}
else
{
h_dist = 50;
}
// by default, look far in the direction the player is facing and slightly upward
cam.xTo = x + facing*h_dist;
cam.yTo = y - v_dist;
//falling camera
if(!grounded)
{
if(yspeed >= 7)
{
cam.xTo = x + facing*peek;
cam.yTo = y + v_dist*1.7;
}
}
// wallsliding camera
if(action == PLAYER_ACTION.wall_slide || action == PLAYER_ACTION.wall_run)
{
cam.xTo = x + facing*h_dist;
cam.yTo = y;
}
//move the camera
cam.x += (cam.xTo-cam.x)/cam_spd;
cam.y += (cam.yTo-cam.y)/cam_spd;
if(input_sprint && state != PLAYER_STATE.defense)
{
h_dist = 130;
}
else
{
h_dist = 50;
}
// by default, look far in the direction the player is facing and slightly upward
cam.xTo = x + facing*h_dist;
cam.yTo = y - v_dist;
//falling camera
if(!grounded)
{
if(yspeed >= 7)
{
cam.xTo = x + facing*peek;
cam.yTo = y + v_dist*1.7;
}
}
// wallsliding camera
if(action == PLAYER_ACTION.wall_slide || action == PLAYER_ACTION.wall_run)
{
cam.xTo = x + facing*h_dist;
cam.yTo = y;
}
//move the camera
cam.x += (cam.xTo-cam.x)/cam_spd;
cam.y += (cam.yTo-cam.y)/cam_spd;
And this the step event for the camera
I'm failling to notice what's making the camera stop working when changing to that specific state and only when moving in it.
yspeed += grav;
if(input_right)
{
xspeed = defense_speed;
}
if(input_left)
{
xspeed = -defense_speed;
}
if((!input_right && !input_left) || (input_right && input_left))
{
xspeed = 0;
}
if(xspeed = 0)
{
action = PLAYER_ACTION.defense;
}
if (xspeed !=0)
{
action = PLAYER_ACTION.defensew;
}
if (input_jump)
{
if(jump_current > 0 && grounded) //Normal jump
{
yspeed = -sqrt(2 * grav * jump_height); //This calculates the required speed to
jump_current--; //reach the desired height.
}
}
//state change
//attack
if(input_atk)
{
image_index = 0;
state = PLAYER_STATE.atk;
exit;
}
//normal
if(!input_defense || !grounded)
{
image_index = 0;
state = PLAYER_STATE.normal;
exit;
}
if(input_right)
{
xspeed = defense_speed;
}
if(input_left)
{
xspeed = -defense_speed;
}
if((!input_right && !input_left) || (input_right && input_left))
{
xspeed = 0;
}
if(xspeed = 0)
{
action = PLAYER_ACTION.defense;
}
if (xspeed !=0)
{
action = PLAYER_ACTION.defensew;
}
if (input_jump)
{
if(jump_current > 0 && grounded) //Normal jump
{
yspeed = -sqrt(2 * grav * jump_height); //This calculates the required speed to
jump_current--; //reach the desired height.
}
}
//state change
//attack
if(input_atk)
{
image_index = 0;
state = PLAYER_STATE.atk;
exit;
}
//normal
if(!input_defense || !grounded)
{
image_index = 0;
state = PLAYER_STATE.normal;
exit;
}
Finally this is the script for the defense state. The camera object is just an empty object that moves around where it's needed, no code in there. Everything is in the create and step events of the player character