Jump to content


Photo

Make view follow object smoothly


  • Please log in to reply
8 replies to this topic

#1 thaiscorpion

thaiscorpion

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 26 June 2012 - 02:21 AM

Okey ive edited this post with more info and links:

Im trying to make a view follow a plyayer smoothly without leaving a specific space on the screen like so:

Posted Image

So far I have got up to a simple script like this:

/* Arguments
    *argument0 = base object
    *argument1 = object to follow
    *argument2 = max distance
*/

//Reasign
    base_obj    = argument0;
    follow_obj  = argument1;
    max_dist    = argument2;

//Create
    dist = abs(base_obj.y - follow_obj.y);   
    speed_dif = follow_obj.vspeed - base_obj.vspeed;
    
if (dist < max_dist) {
    base_obj.vspeed -= 0.1;
    
}if (dist > max_dist) {
    base_obj.vspeed += 0.1;
    
}else {
    base_obj.vspeed = follow_obj.vspeed;
}

This works in a way that one object with follow the other precisly but I want it to follow smoothly like stated above :thumbsup:

Here is an example of what ive got:
Example

Any help or questions just say!
Ty in advance!

-Thaiscorpion

Edited by thaiscorpion, 27 June 2012 - 04:49 PM.

  • 0

#2 Manuel777

Manuel777

    InvaderGames

  • GMC Member
  • 2847 posts
  • Version:GM:Studio

Posted 26 June 2012 - 03:02 AM

Have you tried using the motion planning functions for the objects motion? (mp_linear_path and such)

Ps: The thread title says "Make view follow object", but that code is for an object following another object..? :confused:
  • 0

#3 thaiscorpion

thaiscorpion

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 26 June 2012 - 03:08 PM

Have you tried using the motion planning functions for the objects motion? (mp_linear_path and such)

Ps: The thread title says "Make view follow object", but that code is for an object following another object..? :confused:


Thanks manuel no I have not tried this and I will do so now, Ill tell you the results soon :D

PS: the reason the title says the view is because what I am trying to achieve is that the view will stay fixed on one object in the middle and then that object follow another one smoothly thats the real objective, incase there was an easier way :D
  • 0

#4 Manuel777

Manuel777

    InvaderGames

  • GMC Member
  • 2847 posts
  • Version:GM:Studio

Posted 26 June 2012 - 04:32 PM

You can also have a controller object to make the view go anywhere you want:

target_x = player.x;
target_y = player.y;
// Replace 'player' by the name or id from the instance to follow

dir = point_direction(x, y, target_x, target_y);
dis = point_distance (x, y, target_x, target_y);
if (dis > 256) {
    x = target_x+lengthdir_x(256 , dir+180);
    y = target_y+lengthdir_y(256 , dir+180);
}
else {
    x += lengthdir_x(dis/10 , dir);// If you divide 'dis' by a bigger number, the view will follow slower
    y += lengthdir_y(dis/10 , dir);// Make sure you divide both x and y by the same number :)
}
view_xview = x-(view_wview/2);
view_yview = y-(view_hview/2);

  • 0

#5 thaiscorpion

thaiscorpion

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 27 June 2012 - 01:15 AM

thanks for the help so far manuel I like your approch its works good expect for one problem when the player accelerates it doesnt keep up the distance just gets bigger and bigger until you cant see the player. Any idea how to fix this?
  • 0

#6 WMCD

WMCD

    Spritist

  • GMC Member
  • 1410 posts
  • Version:GM5

Posted 27 June 2012 - 01:55 AM

When the following object gets within a certain distance of the leading object, make it set its speed to the same speed as the leading object, and if it's too far away, make it increase its speed to slightly faster than the leading object in order to catch up. Additionally, you could have it start to slow down if it gets too close. The key here is to relatively increase and decrease the following object's speed so that the following object just doesn't snap to different speed settings.
Distance_to_object is your best friend in this situation.


If you are not sure about how to go about coding this, lemme know and I'll whip up some example code to get you started.
  • 0

#7 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14386 posts
  • Version:GM:Studio

Posted 27 June 2012 - 05:17 AM

1) Make a New object FollowPlayer
2) Make your view follow the FollowPlayer in the room properties
3) There, also set the follow speed to -1 and set the follow boders to be half the view size.
now the view will always center on the FollowPlayer

4) in the FollowPlayer EndStep

x+= (player.x-x)/10;
y+= (player.y-y)/10;

5) drop object in the room

/10 is slower than /5....
  • 1

#8 thaiscorpion

thaiscorpion

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 27 June 2012 - 11:58 AM

Thanks guys for all your help,
WMCD- thats exactly what Im looking for but adding in a maximum distance so it doesnt get to far away, but yes you guessed it I just cant figure out how to do what you described Id really apreciate that example :D

EDIT: updated my first post with more information and an example file to speed things up!

Edited by thaiscorpion, 27 June 2012 - 04:50 PM.

  • 0

#9 thaiscorpion

thaiscorpion

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 28 June 2012 - 10:26 PM

Added lots of new info at top its basicly a new post :D 2 days past! still looking for help
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users