How do I move an object to a random predetermined target?

  • I have been looking on the forums, and google, and playing around with ideas in C3 and haven't been able to figure this out yet. Any help/suggestions/advice would be greatly appreciated.

    What I want to achieve is:

    When the Start Button is clicked, The Unit should move towards a randomly selected Target using 4-directional tile based movement.

    I've created a very minimal C3 project to explain things/give people a way to offer their advice.

    There are 3 objects: a Button, and two Sprites.

    There is a single layout with 6 instances of the Target Sprite, and one instance of the Unit Sprite.

    There is an "empty" event sheet with a condition of clicking the start button, and a comment explaining what should happen.

    Construct 3 Project: https://www.dropbox.com/s/g129whdsi5wo87m/ChoosingARandomTarget.c3p?dl=0

  • I would advise against sharing an empty file and asking users to make something for you in future, that's probably why there was no response to this. I think you could resolve this yourself by picking a location sprite, playing around with the tile movement behaviour and simulate control in a direction combined with comparing whether player x/y are less/more than the location sprite.

  • I have been looking on the forums, and google, and playing around with ideas in C3 and haven't been able to figure this out yet. Any help/suggestions/advice would be greatly appreciated.

    When the Start Button is clicked, The Unit should move towards a randomly selected Target using 4-directional tile based movement.

    If its a random pick then at some point you have a instance picked right? therefore you know/predetermine which instance is picked.

    Example of pseudo code:

    Requires 3 global or local variables one for the X position (target_x) and one for the Y position (target_y)of the randomly picked red sprite instance. And the 3rd variable (moving) is for telling the moving sprite to start moving.

    If moving = 0 the green sprite doesn't do nothing

    If moving = 1 the green sprite starts moving towards the coordinates set at target_x and target_y

    And we go as follows:

    Condition block

    1st condition of the block

    When start button is/was/ pressed/released

    2nd condition on the same block

    pick a random instance of red sprite

    Action

    set target_x to red sprite.x

    set target_y to red sprite.y

    set moving to 1

    Condition block 2

    If moving = 1

    Action Green sprite.set position to target_x target_y

    if you want it to move pixel by pixel and face the direction

    Then we replace the action of green sprite with the following

    Action green sprite.movetowards.angle by 5px at angle(self.x,self.y,target_x,target_y)

    For a grid like movement is a bit more complicated as you need to calculate the x y block and movement of things and length /step for that u can use the grid movement as would be much easier for you to understand, then me explaining a overcomplicated system in pseudo code.

    you might want to take another look for other examples at scirra.com/tutorials if you can't find the answers in the C3 tutorial section or forum. (Google doesn't really help as not all tutorials have visibility in the google ranking as other bigger websites might have better keywording/tagging etc)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is probably a dumb question.

    How do I perform the random pick?

    I am still green with construct even though I have used c2 for a little over a year, off and on to get a single project finished.

    I shared the empty project to visually explain my scenario and let people play around easily. I don't expect anyone to build my game for me, jyst asking for some approaches to solving one aspect of it that I am stuck with.

    I saw other users ask about some mechanic design and they had many users come up with ideas to solve. I guess that it is not an okay approach for a new forum user.

    Thanks for your suggestions. I will try more trial and error instead of asking for help here.

  • How about something like: Targets -> "Pick by unique ID" with a value of: random(Targets.Count) where Targets is a Family containing your target sprites.

    And sure, I think it always helps to show you've tried some things in your question. But I get that when you're starting out with a new product you don't know the terminology or ideas so googling isn't as helpful. But Construct is great and fast for just trying things.

  • I must have overlooked the `Pick random instance` condition in System. :facepalm:

    Thanks again.

  • To anyone who may run across this thread and/or have the same question.

    I've solved what I wanted to achieve, and I'm providing the C3 project for anyone to use/learn from. :)

    Subscribe to Construct videos now

    https://www.dropbox.com/s/3k6fuvwew80fmtl/ChoosingARandomTarget_2.c3p?dl=0

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)