Teleport entities relatively to the center of a block

I just wanted to teleport an entity relatively, but the entity is not exactly in the middle of a block.

For example the coordinates are 0.3, 63.0, 0.76 and I teleport the entity relatively using ~1 ~0 ~1. The resulting coordinated will be 1.3, 63.0, 1.76 but I want the entity to be in the center of the block. So it has to be 1.5, 63.0, 1.5 instead.

Is it possible to achieve this? Thank you!


Solution 1:

First, teleport or summon the entity relatively as you're doing at the moment.

There are a few ways to center an entity on a block. Some of the more clunky methods involve teleporting the entity elsewhere, shoving it against the corner and then teleporting it ~0.3 ~ ~0.3, or similar things.

The method I think works best is by using paintings or item frames. When summoned, they're always at the same location in the block:

http://i.imgur.com/vlq6Zmp.png

They're horizontally centered, and -0.469 off on the Z axis. This means that to center yourself, you can teleport to an item frame, and then teleport ~0.469.

So your order of commands should be something like this:

Your relative teleport/summon
/execute @e[name=Name] ~ ~ ~ /summon ItemFrame
/execute @e[name=Name] ~ ~ ~ /tp @e[type=ItemFrame,c=1,r=1]
/tp @e[name=Name] ~ ~ ~0.469
/execute @e[name=Name] ~ ~ ~ /kill @e[type=ItemFrame,c=1,r=1]

Solution 2:

You can use Leash Knots like this:

/execute @e[name=Name] ~ ~ ~ /summon leash_knot ~ ~ ~ {CustomName:Centre}

/tp @e[name=Name] @e[name=Centre,c=1]

/kill @e[name=Centre]

(This is for 1.11, for 1.10 or lower use LeashKnot instead of leash_knot)

For 1.13+:

/execute as @e[name=Name] at ~ ~ ~ /summon leash_knot ~ ~ ~ {Tags:["Centre"]}

/tp @e[name=Name] @e[tag=Centre,limit=1]

/kill @e[tag=Centre]