# How do I have an objects position always between two others

Get help using Construct 2

### » Sat May 13, 2017 11:31 pm

I have two objects that move and I have another object that I would like to stay in the horizontal and vertical middle of both objects. Lets call the to moving objects leftPlayer and rightPlayer and the middle object spr_middle. I made global variables that sets itself to the horizontal distance and vertical distance between leftPlayer and rightPlayer every tick.

Suppose that the screenWidth is 1000, leftPlayer.X is 250 and rightPlayer is 750. spr_middle should be at 500. Every tick this should adjust itself depending on how leftPlayer and rightPlayer move. After I figure that out I could do the same for the height and the Y axis...

Thanks for any help...this has been driving me crazy...
B
8
S
1
Posts: 4
Reputation: 410

### » Sun May 14, 2017 12:07 am

inbetweenx=(ax+bx)/2
inbetweeny=(ay+by)/2
B
175
S
50
G
200
Posts: 8,627
Reputation: 124,530

### » Sun May 14, 2017 12:16 am

@bizzy401

Maybe something like this, you can drag the objects and see

Capx: https://www.dropbox.com/s/3v44fg3tf35ihs3/TheMiddle_Object.capx?dl=0
B
51
S
31
G
95
Posts: 421
Reputation: 53,228

### » Sun May 14, 2017 1:42 pm

https:// drive.google.com/open?id=0Bzy8n1DGExZvOEVidGRXLUc0WHc <---remove the space after the https://

Here is what I am working on, both methods that were proposed work, but if the leftPlayer and the rightPlayer switch sides it ceases to work properly. I think I may have to make four different "if" statements depending which sides them players are at to get it to =work right

Thanks for all of the help.

The game has to be controlled with a controller for the record...
B
8
S
1
Posts: 4
Reputation: 410

### » Sun May 14, 2017 2:23 pm

Doesn't this do what you want: cameraBox:Set position: (rightPlayer.X+leftPlayer.X)/2, 360
B
71
S
22
G
280
Posts: 3,841
Reputation: 154,384

### » Sun May 14, 2017 2:29 pm

bizzy401 wrote:https:// drive.google.com/open?id=0Bzy8n1DGExZvOEVidGRXLUc0WHc <---remove the space after the https://

Here is what I am working on, both methods that were proposed work, but if the leftPlayer and the rightPlayer switch sides it ceases to work properly. I think I may have to make four different "if" statements depending which sides them players are at to get it to =work right

Thanks for all of the help.

The game has to be controlled with a controller for the record...

Hi,

When you say if the switch sides ceases to work properly what you mean with that can you be more specific, because is working here on my side in the capx I provided you, if I didn't understand wrong the goal was to center the middle object in the center between left object and right object at every thick, is that correct?
if not can you explain more what you trying to accomplish will be easier for someone to give you the right help
B
51
S
31
G
95
Posts: 421
Reputation: 53,228