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 [Solved] Randomly picking an instance's direction

Discussion in 'Programming' started by Biosyn, May 3, 2019.

  1. Biosyn

    Biosyn Member

    Oct 17, 2017

    I'm trying to make a set of projectile objects move toward a set of deflector object instances. The deflectors are arranged in a circle after being spawned through a for loop. It seems to be working except for an issue where the deflected projectiles do not randomize and only choose to move towards one of the deflector object instances (probably the first one created thru the for loop).

    The setup is as follows. A player object creates a set of deflector object instances linked to it:

    def_inst.myid=myid; //myid being the main controller for the player, deflector and projectiles.

    After creating the projectile instances, they are programmed to spawn within the deflector instances arranged in a circle and choose a direction pointing towards one of the deflectors, randomly. It works fine up to this point. Now when the projectile moves toward a deflector and collides with it, I want it to change direction to point towards any of the other deflectors (not the one it just collided with). The end result should be the projectiles colliding against the deflectors and criss-crossing within the circle formation.

    The projectile code for colliding with the deflector:

    if can_deflect==1
        alarm[0]=4 //reset can_deflect
        var inst = instance_find(myid.def_inst, irandom(instance_number(myid.def_inst) - 1));
    As I said earlier, the problem is, after colliding with the deflectors initially, instead of randomly picking and moving towards any other deflector's direction, the projectiles move towards one only.

    Is there any way to achieve this?

    Any guidance is appreciated. Thanks!
    Last edited: May 3, 2019
  2. vdweller

    vdweller Member

    Jun 24, 2016
    Hmmm have you tried using
    Biosyn likes this.
  3. Biosyn

    Biosyn Member

    Oct 17, 2017
    Well I'll be. Thanks for that quick fix! Works as expected.

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