Skip to content

Get Position within trigger?

I'm trying to develop a system that will spawn monsters at positions close to the walls of a room. Instead of using several waypoints, I wondered if it would be possible to use a big trigger and find random positions within it and then, close to its borders. Is there anything similar to "GetPositionWithinTrigger" or a possible workaround?

Comments

  • gpeddinogpeddino Member Posts: 50
    edited September 2019
    Another possibility I'm considering, probably easier, is to place waypoints at the corners of a room and find a way to get a random location between a random pair of corners. But that's proving to be pretty hard as well.
  • ProlericProleric Member Posts: 1,282
    As it happens, trigger geometry is hard to work with.

    The simplest method is to use CreateObject to spawn the monster at the computed location. You can also spawn waypoints if you need them for some reason.

    Since you know the coordinates of the room in advance, you can use the Random function to choose a random wall, then a random x and random y close to that wall.

    A refinement is to place reference waypoints at two diagonally opposite corners, then discover the room coordinates via GetLocation. That way, the script will still work if the area is resized.

    The Random function can also be used to create a "random pair algorithm" if you need it.



  • gpeddinogpeddino Member Posts: 50
    Thanks for the input! I managed to come up with this function which worked pretty well, even though it could probably be improved. I'm just a beginner so it's taking me a while to advance.
    location GetRandomLocationBetweenObjects(object oObject1, object oObject2)
    {
        object oArea = GetArea(oObject1);
        vector v01 = GetPosition(oObject1);
        vector v02 = GetPosition(oObject2);
        float fRandom = IntToFloat(Random(100)) * 0.01;
    
        if (v01.x != v02.x)
        {
            float a = (v02.y - v01.y)/(v02.x - v01.x);
            float x = (v02.x - v01.x) * fRandom + v01.x;
            float y = (x - v01.x) * a + v01.y;
    
            return Location(oArea, Vector(x, y), 0.0f);
        }
        else
        {
            float x = v01.x;
            float y = (v02.y - v01.y) * fRandom + v01.y;
    
            return Location(oArea, Vector(x, y), 0.0f);
        }
    }
    
Sign In or Register to comment.