1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 Movement and rotation help

Discussion in 'Programming' started by CrackedGamer§, Dec 8, 2018.

  1. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    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. : /
     
  2. Haulidaie

    Haulidaie Member

    Joined:
    Dec 9, 2018
    Posts:
    13
    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.
     
  3. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    So what I mean by "same origin" is that I'll just make their co-ordinates the same. And I mean Direction.
     
  4. Haulidaie

    Haulidaie Member

    Joined:
    Dec 9, 2018
    Posts:
    13
    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;
    }
     
  5. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    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?
     
  6. Haulidaie

    Haulidaie Member

    Joined:
    Dec 9, 2018
    Posts:
    13
    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"?
     
  7. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    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?
     
  8. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    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?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice