Multi-Resolution Game Development With GameBuilder Studio Check it out!
NEW Version 0.9.7 has arrived! New Blazing Fast Particle Engine & More Game Actions. Check it out
How to let two spawned objects face each other?
  • Let's say I have soldiers which the player can spawn (as long, as he has gold or something) and enemys spawning to certain game rules (like time, difficulty...). I want these two to face each other and fight on collision of their collision spheres (or detection spheres in this case). Is there a way to let one object move to the colliding objects coordinates (minus 20 pixel in the x or something)? The solution has to work for several objects on screen (multiple fights going on).
    In my mind is an idea regarding collision-slots in a global datacontainer, which saves the coordinates of colliding soldiers. But this would be hard to implement and it would limit the simultaneously fights to the number of slots I implement. And the Logic to check for which slot is free to put data in and to read out by another entity just makes my mind explode...

    I have a feeling that the new functions "FindEntity" and "Game.CurrentActionData.foundEntity" together with "MoveToward" may come in handy. But I failed to apply them in a working manner.
  • 8 Comments sorted by
  • @Cavaron, Have you tried having two separate spawns one left facing and one right facing? 

    If its the same animation or spatial in order to reverse it, I believe you could change the width of it to a negative and that should create a mirror like spatial.

  • @Cavaron are you building something that is a top down view or more of a side scrolling style? If you want to flip something on the x axis so that it changes the direction it is facing you could just multiply the scale.x property of the renderer by -1. (make sure to use setPoint(Self.Renderer.scale.x * -1, Self.Renderer.scale.y) in an expression because scale is a point object)

    If you want to rotate an object in a top down style game to face another object you could use the new Rotate Toward action and turn on snapping.

    @Cavaron Couple other questions:

    Will you be spawning only two objects at a time to fight at once? 
    Will you be spawning a bunch of soldiers and you want one side to attack another? (This changes logic significantly. Also we may need to add one more flag to The FindObjectInRange action to only search along one axis. That is not supported currently)

  • Thanks for your replies!
    Ok, maybe it's easier to show a picture of what I'm trying to do, red arrows show where the entities are supposed to be for my purposes - of course they can go there in a straight line ;)


    Thing is, the humans are supposed to stop on contact (which they do right now) and the orks are supposed to walk to them (same point on y, but about 20 pixels away on the x), so they can fight. If the ork wins, he is supposed to go for another human. How would you do a logic that a) tells the ork to which coordinates he has to move and b) in which direction each of the entities has to "look".

    Setting scale.x -1 is a good trick to let them look in the other direction (and safe some sprite images), I already use it while moving - a rule is checking for the x-velocity of the entity and changes the scale.x accordingly. But thanks for the hint.

  • @Cavaron to determine the direction the ork has to look you can use the velocity to determine the direction to face which you are doing and it works while the ork is moving to the target. For the human who is stationary to determine which direction to face when fighting an ork you can use an expression and calculate the angle between the human and the ork it is fighting:

    abs( rotationOfAngle( Entity.Ork1.position , Self.Spatial.position ) )

    The abs() helper method will provide a whole number without the negative sign. So you always know that if the angle value is less than 90 degrees then the human should be facing to the right and if the angle is greater than 90 the human should be flipped to face the left. Is that what you are asking?

    I'm assuming you know how to use the Find Object In Range action and the Move Toward action to have the Ork find the closest human object and move toward it with a stopping distance. If not I can explain that as well. 

    Also we can add an option to the Move Toward action to have it only move along a certain axis but that would be in the next release.
  • @Lavon thanks for your reply!

    "Entity.Ork1.position" - let's say there is more than one spawned ork of the same entity. How does the human know which one to face? I could also do a "Find Object In Range" rule for that - but theoretically there could be another ork closer to the human but already occupied in another fight.

    Yes, a detailed description of how the two actions (Find Object In Range action and the Move Toward) work would be great. I couldn't find any in the documentation-section, of course it's a newly implemented action, so maybe that's coming soon. My approach doesn't seem to work so far:
    Maybe I got the wrong impression of how "In Range of Spatial" and "Search Distance" work or something. I have the feeling that the ork tries to walk to the human (walking-animation gets activated) but he really doesn't move. Is the target of the "Move Towards" action the center of the spatial, or is it the border of the CollisionShape? Because in the second case he is like already there, when the two CollisionShapes collide...

    Another question - the humans walk along many waypoints on the road, for which I use the "Move Towards" action. It would be great to be able to define the target of the "Move Towards" action with an expression. Like #Waypoint"variable".Spatial. The way it is know, I have a rule for every waypoint - but maybe there is a better way to do it and I just can't see it.
  • @Cavaron yes these haven't been documented yet my apologies. 

    The Find Object In Range will continuously (every frame) search using filtering for one Spatial within a certain pixel distance of the specified Spatial and it will trigger the nested action every frame after populating the global CurrentActionData property with the found Spatial. Yes this search is done from the center point of the supplied Spatial. 

    The action in your image looks like the Find Object action which is triggered only once. The Find Object action is triggered only once and it does a quick search for all objects that match the search criteria within a search distance and it will trigger the nested action once either "For Each" object found or once for a "Random" object in the list of objects found if there is more than one. You can optionally sort the search results by closest spatial.

    You need to understand the order of execution when it comes to actions and their nested actions. If you put an action inside of a condition wether it is a persistent or one-time action it will only fire as long as that condition is valid. Once the condition it is under becomes non-valid it stops everything unless the nested action has a flag that will allow it to continue running even after being stopped. (i.e. like the "Run to Completion" flag on an Interpolate action). So if you are triggering a persistent action like the Move Toward action and it has been stopped by the condition it is under then the object won't appear to move. 

    Another thing I would check is what type of collision object do you have your objects set to because that can effect if a Spatial is moveable or not.

    The Move Toward action is a persistent action that fires every frame and it sets the linearVelocity of a Spatial toward a supplied target Spatial's position. The linearVelocity of Collision Spatials are ignored if the type is set to Kinematic.

    Yes there is a better way to specify a dynamic spatial for the Move Towards action. All you have to do is assign the next waypoint spatial object to a property on the Data container (i.e. "NextWayPointSpatial") and point to it via the Target Spatial property (i.e. @Properties.NextWayPointSpatial) reference.

    Data components are unsealed dynamic objects so properties can be created on it at runtime and the type is not strictly enforced at runtime. So you can create the property "NextWayPointSpatial" and specify that it is a String in the editor but if you assign a Spatial object to it everything should work fine. :)
  • @Lavon Oh, wow... you are absolutely right. I somehow assumed the Move Toward would be done until completion. Man, that kept me up at night - thanks for restoring my ability to sleep well ;) And thanks for the waypoint hint too!

    One suggestion - it would be great to let the Move Toward action work with x and y coordinates too, which can easily be red out of the foundSpatial. This way, moving the ork to the same y and a litte offset on the x of the human would be piece of cake to implement.
  • @Cavaron ok I will see about adding an offset per axis as well.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion