[SOLVED] How to make camera fallow an object?

A

AUGE

Guest
I just wonder, how do I make camera stay on an object and only move when object moves. When I tried to do so, the camera would accelerate by the speed of the distance from mouse to the middle of the view. Any suggestions, please.
 
A

AUGE

Guest
The thing is it is not fallowing all the time, only on some conditions. Otherwise crosshair object is in another place. I just want to know a code that I could use to what I said in first place ;)
 

RyanC

Member
I would just use something like this:

view_xview[0] = lerp(view_xview[0],obj_player.x-(view_wview[0]*0.5),0.05)
view_yview[0] = lerp(view_yview[0],obj_player.y-(view_hview[0]*0.5),0.05)

just change obj_player to your cross_hair object or what ever object you want to follow smoothly and change 0.05 to be faster or slower.
 
A

AUGE

Guest
I would just use something like this:

view_xview[0] = lerp(view_xview[0],obj_player.x-(view_wview[0]*0.5),0.05)
view_yview[0] = lerp(view_yview[0],obj_player.y-(view_hview[0]*0.5),0.05)

just change obj_player to your cross_hair object or what ever object you want to follow smoothly and change 0.05 to be faster or slower.
Ok, this works, but I need some more help here. How do I make the crosshair only move when mouse moves and later after it's goes back to normal (where crosshair tracks mouse) how do I make mouse in the middle and not somewhere moved in the corner while aiming with your code?
 

RyanC

Member
Can you not just say:
obj_cosshair.x = mouse_x
obj_cosshair.y = mouse_y

I'm going to need a little more information on what your trying to do exactly?
 
A

AUGE

Guest
well, I just want to keep mouse in the middle whatever would happen, so that when you are done aiming, mouse would be in the middle of the view. I have nothing to tell more. Everything else is said above ;)
 
A

AUGE

Guest
Can you not just say:
obj_cosshair.x = mouse_x
obj_cosshair.y = mouse_y

I'm going to need a little more information on what your trying to do exactly?
This would make the camera ccelerate the each time I move mouse. I want camera to move a bit after I move mouse, and then reset camera move speed to 0 and mouse to current position of the crosshair.
 

RyanC

Member
If you used lerp again for that with a slower speed it would do what your asking I think.
Hey check out this tutorial that includes camera movement with mouse too, then just lerp your cross-hair to mouse position.


What code do you have in step event at the moment for all this?
 
Last edited:
A

AUGE

Guest
By now I have this code:
Code:
    if mouse_check_button(mb_right)
    {
        global.aiming=true;
    }
    else
    {
        global.aiming=false;
    }
    
    if global.aiming==false
    {
        x = mouse_x;
        y = mouse_y;
        old_x=mouse_x;
        old_y=mouse_y;
    }
    else
    {
        //window_mouse_set(window_get_width() / 2, window_get_height() / 2);

        x = mouse_x;
        y = mouse_y;
       
        view_xview[0] = lerp(view_xview[0],x-(view_wview[0]*0.5),0.1)
        view_yview[0] = lerp(view_yview[0],y-(view_hview[0]*0.5),0.1)
       
    }
As you can see if the mouse button is pressed it is aiming and that is when I want it to move only when you move your mouse.
Now if I uncomment the window_mouse_set it works, but it is working with bugs. It teleports the crosshair and is very sharp looking :/
 
A

AUGE

Guest
SOLVED it!

Code:
    x += mouse_x - old_x;
    y += mouse_y - old_y;
    
    old_x=mouse_x;
    old_y=mouse_y; 
      
    view_xview[0] = x - (view_wview/2);
    view_yview[0] = y - (view_hview/2);
    
    window_mouse_set((window_get_width() / 2), (window_get_height() / 2));
 
Top