GameMaker Yet another camera problem

D

DustyHill

Guest
Hey guys, I have a problem with my camera. I created the "between player and cursor" object, using "mean()" function:

Step Event (object between player and cursor):
Code:
meanX = mean(PLAYEROBJ.x, mouse_x)
meanY = mean(PLAYEROBJ.y, mouse_y)

x = meanX
y = meanY
Ok?

So, the problem is speed of the camera...

If speed of the camera is equal to speed of the player (I mean, I use x+=PLAYERSPEED (Step Event) to move the player, not hspeed), then if, for example, player go to right, I can't move camera to the same direction. (Obviously, because speeds are equal to each other)

If speed of the camera is faster, than speed of the player, then view starts twitch.

And if speed of the camera is slower, than speed of the player, camera can't "catch" the player and player is getting away from view.

May be, this way to do it is completely wrong...
 

samspade

Member
I'm not sure I understand your question. You don't have speed anywhere in the code you posted. You're directly setting the camera's position. So if speed is affecting your camera's position that means there is other code that should posted.

Assuming there is other code about speed, there are many solutions but the most common one is something like:

Code:
x += (target.x - x) * 0.25;
y += (target.y - y) * 0.25;
(which I'm doing from memory so I might be slightly wrong). In other words the farther away the object is the faster it will move. The closer, the slower.
 
D

DustyHill

Guest
I'm not sure I understand your question. You don't have speed anywhere in the code you posted. You're directly setting the camera's position. So if speed is affecting your camera's position that means there is other code that should posted.
Ah it seems I forgot to say, that camera follows object between player and cursor, let's just call it mean object and speed of the view [0] is standart - (-1).
When cursor moves, it changes x,y of the mean object smoothly and camera moves smoothly too. But when player object starts move, incerasing or decerasing its x,y camera moves really horrible.

I forgot I also tried to use camera_set_view_speed() to change speed of the view.

But now I just can't understand what the problem is.
 

samspade

Member
Again, you'll need to post your code if you want more accurate help. But don't use camera_set_speed. You could make it work for this problem, but it would be much more work than just setting the position of the camera yourself.

With that though I can be a little more specific.

Code:
//camera step event if you are using a camera object
x += (target.x - x) * 0.25; //target should be the instance id of mean object
y += (target.y - y) * 0.25; //target should be the instance id of mean object
camera_set_view_pos(your_view, x - view_get_wport(your_view)/2, y - view_get_hport(your_view)/2);
I would also recommend watching this: GMS2 Cameras: As Simple as Possible.
 
D

DustyHill

Guest
I think I got it...

So, detailed code:

Player Create:
Code:
playerSpeed = 2;
Player Step:
Code:
//Condition to move
{
x or y (+ or -)= playerSpeed;
}

Mean object Step:
Code:
mediumMeanX = mean(PLAYEROBJ.x, mouse_x);
mediumMeanY = mean(PLAYEROBJ.y, mouse_y);

x = mediumMeanX;
y = mediumMeanY;

I also can use camera object or just make view follow mean object (view speeds = -1 in room properties)

If I use camera object, camera object Step:
Code:
x += (MEANOBJ.x - x)
y += (MEANOBJ.y - y)
But it ins't necessarily...

Sooo, If playerSpeed is an integer, the camera does't twitch...
 
Last edited by a moderator:
Top