GML Camera follow not working for me

D

deathwish

Guest
Hey guys,

I just sort of got into GML and I'm following shaun spalding's tutorial on making a platformer to get started, it's going well so far I understand how the stuff works and have been able to apply certain things in my game that aren't in the tutorial just based of the logic of it.

Regardless; I'm up to the part where he explains how to set up the camera to sort of zoom in and follow the player as they go along and I've got a problem.

So when I set the camera up I had the object following setting in the actual room editor on to my player object

I did all the coding checked if it was the same as on the tutorial etc and I cant see any errors there; but when I tried it out it started off like this:



As my character (grn guy) moves more to the right the camera suddenly moves and starts working as it should but only when im about a third of the way in to room:



Upon troubleshooting I realised that in the video shaun doesn't set and object to follow in the actual room editor; I assumed this would be the fix but when I turned it off the camera was even worse:



as I moved to the right the camera would angle down as it did with an object set to follow but would also exponentially continue going down as I went further to the right:



I can't seem to figure out where I went wrong hopefully you guys can help.
For information:

My room hxw = 1536x648
Camera and viewport = 768x432

The code that was used

Create step:
Code:
Cam = view_camera[0];
follow = obj_player;
view_w_half = camera_get_view_width(Cam) * 0.5;
view_h_half = camera_get_view_height(Cam) * 0.5;
xTo = xstart;
yTo = ystart;
Code:
//Update destination

if (instance_exists(follow))
{
    xTo = follow.x;
    yTo = follow.y;   
}

//Update obj position
x += (xTo - x) / 25;
y += (xTo - y) / 25;


x = clamp(x,view_w_half,room_width-view_w_half);
//update cam position
camera_set_view_pos(Cam,x - view_w_half,y - view_h_half);
Thanks!!
 
D

deathwish

Guest
First line of code was create event; second was step event (tried to edit original post but it's spam apparently)
 
D

deathwish

Guest
bump because this took forever to get approved for no spam
 
Well your problem is here:
Code:
//Update obj position
x += (xTo - x) / 25;
y += (xTo - y) / 25;
You're incrementing the value of y with the horizontal position of your object, but you want to increment by the vertical position: Change 'xTo' to 'yTo' in the second line of code.


I'd advise getting more comfortable with programming languages; Don't just watch one video but reference many sources of information so you can see how different methods work. This will teach you how you can create your own version, which will give you a greater understanding of what is wrong when it's not working.

Hope this fixed your problem. :)
 
D

deathwish

Guest
Well your problem is here:
Code:
//Update obj position
x += (xTo - x) / 25;
y += (xTo - y) / 25;
You're incrementing the value of y with the horizontal position of your object, but you want to increment by the vertical position: Change 'xTo' to 'yTo' in the second line of code.


I'd advise getting more comfortable with programming languages; Don't just watch one video but reference many sources of information so you can see how different methods work. This will teach you how you can create your own version, which will give you a greater understanding of what is wrong when it's not working.

Hope this fixed your problem. :)
Well damn that would be the problem I must have glossed over that completely - thank you

And yeah it's a process that I'm going through now I'm not trying to make a game completely from the start it's just practice.
 
Top