The View follows the character and shakes but not at the same time [SOLVED]

wilmer

Member
Greetings to all:
Trying to summarize, Through code I make the View follow the player and when he throws a grenade and explodes a shaking effect is created, the problem is that with my code the View has to wait for the shaking effect to end to continue to the character, causing a sudden movement of the View. How can I avoid seeing that Cut seen in the Video?


This is All the code

obj_Control // Object that makes the view follow the player
min_view_x=0
min_view_y=0
max_view_x=room_width-camera_get_view_width(view_camera[0]);
max_view_y=room_height-camera_get_view_height(view_camera[0])
if obj_Shaking.CanShake == false
{
halfViewWidth = camera_get_view_width(view_camera[0])/2;
halfViewHeight = camera_get_view_height(view_camera[0])/2;

cx=obj_parentpp.x-halfViewWidth
cy=obj_parentpp.y-halfViewHeight

cx=clamp(cx,min_view_x,max_view_x)
cy=clamp(cy,min_view_y,max_view_y)

camera_set_view_pos(view_camera[0],cx,cy )
}

obj_Granade // When this object touches the ground it explodes and the shaking effect is activated
if place_meeting(x,y,objBlock) //This is the object of the Floor
{
if instance_number(obj_Granade) == 0
{
instance_create(x,y-20,obj_GranadeExplosion) //is the object or sprite of the explosion
}


obj_Shaking.alarm[0]=0.4*room_speed
if obj_Shaking.alarm[0] > 0
{
obj_Shaking.CanShake = true
}
instance_destroy();
}

obj_Shaking
shake_x=camera_get_view_x(view_camera[0])
shake_y=camera_get_view_y(view_camera[0])

CanShake = false
//Check when you can shake
if (CanShake)
{
camera_set_view_pos(view_camera[0],shake_x + irandom(6) ,shake_y + irandom(6) )
}

///Shake
if (CanShake) exit;

shake_x = camera_get_view_x(view_camera[0]);
shake_y = camera_get_view_y(view_camera[0]);
camera_set_view_pos(view_camera[0],shake_x,shake_y)
CanShake = false;
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Don't have a separate object for screenshake. Just use the obj_Control events! You already have the code in there so use it... EG:

GML:
// CREATE EVENT
min_view_x=0
min_view_y=0
max_view_x=room_width-camera_get_view_width(view_camera[0]);
max_view_y=room_height-camera_get_view_height(view_camera[0]);
shake = false;
shake_magnitude = 0;

// STEP EVENT
halfViewWidth = camera_get_view_width(view_camera[0])/2;
halfViewHeight = camera_get_view_height(view_camera[0])/2;

cx=obj_parentpp.x-halfViewWidth
cy=obj_parentpp.y-halfViewHeight
cx=clamp(cx,min_view_x,max_view_x)
cy=clamp(cy,min_view_y,max_view_y)

if shake
{
cx = (cx - shake_magnitude) + random(shake_magnitude * 2);
cy = (cy - shake_magnitude) + random(shake_magnitude * 2);
shake_magnitude -= 0.5;
if shake_magnitude <= 0
    {
    shake = false;
    }
}

camera_set_view_pos(view_camera[0],cx,cy )
Now, when your grenade explodes, simply set the shake variables in the controller...

GML:
with (obj_Control)
{
shake = true;
shake_magnitude = 5;
}
This should work perfectly for what you want. Also, I wrote a tech blog about this for YYG some time ago, if it's any help!

 

wilmer

Member
Don't have a separate object for screenshake. Just use the obj_Control events! You already have the code in there so use it... EG:

GML:
// CREATE EVENT
min_view_x=0
min_view_y=0
max_view_x=room_width-camera_get_view_width(view_camera[0]);
max_view_y=room_height-camera_get_view_height(view_camera[0]);
shake = false;
shake_magnitude = 0;

// STEP EVENT
halfViewWidth = camera_get_view_width(view_camera[0])/2;
halfViewHeight = camera_get_view_height(view_camera[0])/2;

cx=obj_parentpp.x-halfViewWidth
cy=obj_parentpp.y-halfViewHeight
cx=clamp(cx,min_view_x,max_view_x)
cy=clamp(cy,min_view_y,max_view_y)

if shake
{
cx = (cx - shake_magnitude) + random(shake_magnitude * 2);
cy = (cy - shake_magnitude) + random(shake_magnitude * 2);
shake_magnitude -= 0.5;
if shake_magnitude <= 0
    {
    shake = false;
    }
}

camera_set_view_pos(view_camera[0],cx,cy )
Now, when your grenade explodes, simply set the shake variables in the controller...

GML:
with (obj_Control)
{
shake = true;
shake_magnitude = 5;
}
This should work perfectly for what you want. Also, I wrote a tech blog about this for YYG some time ago, if it's any help!

Incredible, Thank you so much for the help it worked for me this 10 out of 10.
 
Top