GameMaker Movement and rotation help

C

CrackedGamer§

Guest
I have two objects, I want obj_a to do two things:

A: Move to the closest instance of obj_b
B: Rotate to still be on the same origin as obj_b AND face the same way as the closest instance of obj_b

I am really having trouble getting this to work... I am kinda new to GMS although I have about 300h across GM:S and GMS 2 according to Steam. : /
 
H

Haulidaie

Guest
What do you mean by "same origin," and by "face the same way" do you mean image_angle or direction?
Happy to help, just need more information.
 
C

CrackedGamer§

Guest
What do you mean by "same origin," and by "face the same way" do you mean image_angle or direction?
Happy to help, just need more information.
So what I mean by "same origin" is that I'll just make their co-ordinates the same. And I mean Direction.
 
H

Haulidaie

Guest
Cool, here ya go. I use point_distance instead of distance_to_object because it'll wait until the origin points of both objects meet, rather than snapping as soon as they collide. Hope this helps. Let me know if you have anymore questions.

Code:
//Find the nearest object of desired type
var nearest = instance_nearest(x, y, obj_b);
//Check distance between obj_a origin and obj_b origin
if(point_distance(x, y, nearest.x, nearest.y) > speed) {
    //Move towards nearest obj_b
    direction = point_direction(x, y, nearest.x, nearest.y);
    speed = 3;
} else {
    //Stop and lock to the obj_b
    speed = 0;
    x = nearest.x;
    y = nearest.y;
    direction = nearest.direction;
}
 
C

CrackedGamer§

Guest
Cool, here ya go. I use point_distance instead of distance_to_object because it'll wait until the origin points of both objects meet, rather than snapping as soon as they collide. Hope this helps. Let me know if you have anymore questions.

Code:
//Find the nearest object of desired type
var nearest = instance_nearest(x, y, obj_b);
//Check distance between obj_a origin and obj_b origin
if(point_distance(x, y, nearest.x, nearest.y) > speed) {
    //Move towards nearest obj_b
    direction = point_direction(x, y, nearest.x, nearest.y);
    speed = 3;
} else {
    //Stop and lock to the obj_b
    speed = 0;
    x = nearest.x;
    y = nearest.y;
    direction = nearest.direction;
}
This is great! I only have one question, what if two of obj_b collide? will there be a chance that obj_a "switches" so to speak?
 
H

Haulidaie

Guest
obj_a will always move towards the nearest obj_b. This applies every step, so if a different obj_b moves closer than the obj_b that obj_a is moving towards, then yes, obj_a will switch to the nearest obj_b.

...I realize that's a lot of "obj_a"'s and "obj_b"'s. Hopefully it's still understandable.

Did you not want obj_a to be able to "switch"?
 
C

CrackedGamer§

Guest
obj_a will always move towards the nearest obj_b. This applies every step, so if a different obj_b moves closer than the obj_b that obj_a is moving towards, then yes, obj_a will switch to the nearest obj_b.

...I realize that's a lot of "obj_a"'s and "obj_b"'s. Hopefully it's still understandable.

Did you not want obj_a to be able to "switch"?
Sorry about the late reply. It would be nice if obj_a will latch onto an obj_b without another obj_a and then stay on that until it is destroyed. That made the code 10 times more complicated, didn't it?
 
C

CrackedGamer§

Guest
And idk if this is the case for the other code but will obj_a face towards or away from the obj_b it is following?
 
Top