• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

setting the camera position not working

TheOnlyWRT

Member
Hey guys!

So in order to scroll around in my game, i am trying to set the camera position using the following code, but it doesnt seem to be working... here is the code

Code:
if(keyboard_check(vk_left)){
    
    //viewX -= 5;
    camera_set_view_pos(0, camera_get_view_x(0)-10, camera_get_view_y(0));
    //view_set_xport(view_camera[0], view_get_xport(0)-5);
    
} else if(keyboard_check(vk_right)){
    
    //viewX += 5;
    camera_set_view_pos(0, camera_get_view_x(0)+10, camera_get_view_y(0));
    //camera_set_view_speed(view_camera[0], 5, 0);
    
} else if(keyboard_check(vk_up)){
    
    //viewY -= 5;
    camera_set_view_pos(view_camera[0], camera_get_view_x(view_camera[0]), camera_get_view_y(view_camera[0])-10);
    //camera_set_view_speed(view_camera[0], 0, -5);
    
} else if(keyboard_check(vk_down)){
    
    //viewY += 5;
    camera_set_view_pos(view_camera[0], camera_get_view_x(view_camera[0]), camera_get_view_y(view_camera[0])+10);
    //camera_set_view_speed(view_camera[0], view_get_xport(0)+5);
    
}
thanks for the help!
 
Have you got views enabled?

Are you sure you want to be using the number 0 as the first parameter in those functions. I.e. check the manual.
 

TheOnlyWRT

Member
Yes, views are enabled as i have a view set up that works just fine. I tried it with both 0 and view_camera[0] and neither of them are working....
 
Are you creating the camera via code, or have setup the camera in the room editor?

I notice in the current code you have above, you have mixed and matched between using "0" and view_camera[0]. Is that just for testing purposes?

EDIT: When you say not working - do you mean its not moving at all...or moving incorrectly.
 

TheOnlyWRT

Member
views are created by code in the room start event. It has switched between "0" and "view_camera[0]" as i was trying different methods. And when I say it doesnt work, the view wont move at all.
 
When I setup a camera and view using the room editor settings, and then use your above code with "view_camera[0]" it works fine.

Post your Room Start code for setting up the camera/view.
 

TheOnlyWRT

Member
here is the code:
Code:
/// @description Insert description here

_index  = 0;//index of the view you want to target
_width  = 960;//set the value port width
_height = 540;//set the value port height


view_enabled = true;//enable the view
view_set_visible(_index, true);//set it to be visible
view_set_wport(_index, _width);//assign the port width
view_set_hport(_index, _height);//assign the port height

//resize game window and center it
window_set_rectangle(display_get_width()/4/*-view_wport[_index]/2*/,display_get_height()/4/*-view_hport[_index]/2*/,_width,_height);
//resize the application surface (for gui purposes)
//surface_resize(application_surface,_width,_height);
surface_resize(application_surface,display_get_gui_width(),display_get_gui_height());

//create a camera
camera = camera_create_view(0, 0, _width, _height, 0, 0, 1, 1,_width/2, _height/2);

//assign the camera
view_set_camera(view_camera[_index],camera);

//set the gui size to match the view
display_set_gui_size(960, 540);
 

TheOnlyWRT

Member
UPDATE: I made the camera in the room editor window, but i can only scroll the camera up and down, it wont ever scroll right and left...
 
S

Samx89

Guest
Create Script

Script name- scp_camera

add this to script-
// Player Camera View
view_xview[0] += ((x-(view_wview[0]/2.2)) - view_xview[0]) *0.2;
view_yview[0] += ((y-(view_hview[0]/2.2)) - view_yview[0]) *0.2;


Object_Player-

step event-
// Camera Follow Player
scp_camera()
 

TheOnlyWRT

Member
where do i add this script? And also view_xview[0] does not exists in GMS2. And there is no player to follow, you can simply scroll around the world using the arrow keys
 
reate Script

Script name- scp_camera

add this to script-
// Player Camera View
view_xview[0] += ((x-(view_wview[0]/2.2)) - view_xview[0]) *0.2;
view_yview[0] += ((y-(view_hview[0]/2.2)) - view_yview[0]) *0.2;


Object_Player-

step event-
// Camera Follow Player
scp_camera()
He's using GMS 2, unforunately this won't work as it's using GMS1.4 camera code.

@TheOnlyWRT

You used this in your Create Code:
view_set_camera(view_camera[_index],camera);

But it needs to be this according to the manual:
You need to use view_set_camera(0, camera);

UPDATE: I made the camera in the room editor window, but i can only scroll the camera up and down, it wont ever scroll right and left...
Did you change the references to "0" in your camera_set_view_pos() functions to view_camera[0]? Otherwise the code you posted above for the Step Event won't work.
 

TheOnlyWRT

Member
Yes, i changed the code from "0" to "view_camera[0]" and it still istn working. The camera will scroll up and down, but for some reason it will not go left and right....
 
You didn't replace all the 0's in your code I would bet. I had the same problem when testing your code.

Replace all the 0's!
 
Top