How can I make a spectator mode in Bedrock Edition via Commands?

Java has a game mode titled "Spectator" where one view the world as a ghost-like form, going through walls and able to "possess" players' bodies and see what they would see on their screen, without the person being able to tell.
Can the "Spectate Player" part be obtained in Bedrock Edition?


Solution 1:

What's Possible

I will share how to make the spectate player part via commands, because a usable, non-nuisance no-clipping through blocks is not (that I am aware) possible in Vanilla Minecraft Bedrock. This will not show how to use a system such as item-dropping to change the player you spectate, as you will have to do that part yourself.

Real Quick

I will be using the selector

@a[tag="viewed"]

and referring to them as viewed for the player being watched, and

@a[tag="spectator"]

for the spectator, which I will refer to as spectator. Use the /tag command to apply and remove tags from entities.

Approach

You will teleport an invisible player behind the spectating player, depending on the viewed's looking position. How do we know someone's look position?

^ ^ ^

These carets each mean something. It is kinda confusing, but for this tutorial we focus on that last caret. ^ ^ ^2 refers to the block in front of where the player's head is looking, 2 blocks in front of the head. This means that if we use a negative value (i.e. ^ ^ ^-3) we get the blocks behind the player's looking location.
(Do not confuse this with tildes ~! They are separate, as tildes just return the block the player's legs are in.)

How

We will use /execute along with /tp. Make a repeating command block; grab a repeating command block by entering this command into your chat window:

/give @s repeating_command_block

Put this command into it:

execute @a[tag="viewed"] ~ ~ ~ tp @a[tag="spectator"] ^ ^ ^-4 facing ^ ^ ^1 false

What did we do? Well first, we used the execute command for the sole purpose of getting access to those carets (^ ^ ^) we use for look location. We are getting the look location of the player with tag viewed. (Remember, you can change the selector as you desire). We put -4 on the end to get the 4 blocks behind that player's look direction. You can adjust this if you want to, just use a negative number. The facing and the more carets just tell us where our spectator should be looking, which is right where the viewed is looking. The false is to prevent the hazard test (you don't need to understand this).
The reason we must use a negative value is because if the spectator is in front of the viewed, the viewed wouldn't be able to place blocks, open chests, break blocks, as they would always hit the spectator!

When the Person starts Spectating

Before that person starts spectating, you need to use these commands on them, you could do this when you first give them the spectator tag.

effect @a[tag="spectator"] invisibility 99999 255 true

Make them invisible for a while, and show no particles.

gamemode @a[tag="spectator"] a

You don't want them breaking/placing blocks, do you?

effect @a[tag="spectator"] resistance 99999 255 true

Make them immune to damage.

effect @a[tag="spectator"] effect weakness 99999 255 true

So they can't hit entities, they won't even deal any knockback or be able to break armor stands!
Also make sure they do not have a bow, armor, milk, potions etc., if you need to be sure use /clear.

You're Ready!

Once you have a system in place for who is being viewed, spectated, and the effects are given accordingly, set the repeating command block's Needs Redstone select box to Always Active.

Limitations

  • If the viewed looks up, the spectator will be tped underground and although they cannot suffocate, they will be looking at dirt or whatever block until you level your vision.
  • If the spectator finds a way to block someone attacking the viewed, they could act as a human shield, this is why I like to teleport them back a fair amount, that way its harder to block PVP.

Solution 2:

As a follow up to @Penguin

If you have the world on Education Edition you can run a command to allow the user to fly.

You can do: /ability @p mayfly true

To get them out of fly mode you first must do /ability @o mayfly false. Then you must kill them because just taking away the ability will not take them out of fly.

Also if you would want to mute the user in chat to prevent them for letting other users know what is going on you can do: /ability @p mute true

To unmute them, simply do /ability @p mute false