How to make a player take control over a living entity

Started by GodDoge on

Topic category: Help with Minecraft modding (Java Edition)

Joined Feb 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to make a player take control over a living entity
Fri, 12/08/2023 - 02:37 (edited)

So, I've been working on a mod for some time and i want to add an entity that is controllable using an item, making the player see the entities perspective and actually control it. but a few things i need help with. But they are either outdated or didnt work.

 

1.Movement and Directional procedures

I want the entity to move around using custom keybinds while the player is invisible floating over the entities shoulder, and a key to stop controlling it.

 

2.Item Use

The entity should be able to use items how a player should (ex. building, use shield, swinging a weapon).

 

3.Alternative Inventory (If possible)

The entity should have its own inventory that the player can use, instead of using his/her items.

 

In Conclusion, I feel like i explained everything inaccurately, but its the best i can do. if you find a way to do any of this, please share a visual as it may solve many other forums. thanks

Edited by GodDoge on Fri, 12/08/2023 - 02:37
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This is a very complicated…
Fri, 12/08/2023 - 12:17

This is a very complicated prompt, but I don't think it's impossible. That being said, here's some rough ideas:

  • There are a couple options for moving the entity. You could use the 'attempt to make entity navigate' function, which runs the default pathfinding, but this can be kind of annoying and unpredictable. The better option would essentially be to rebuild the player's controls from scratch: Create new keybinds, (probably using the arrow keys), and for each key make a procedure that adds velocity to the controlled entity using the 'attempt to override motion vector' function. 
    • This is easy enough for forwards velocity: you need to essentially take the x and z look angle vectors of the player, (this is kind of hard to explain, but they're essentially the x and z values of the direction the player is looking), multiply them by your desired speed, and set the controlled entity's velocity to that amount. (Or plus a very small amount if you want them to accelerate, probably clamped within a certain range.) You do not want to do the same thing for the y velocity, unless you want the controlled entity to be able to fly.
    • For backwards velocity, you do the same thing with the look angle vectors, but also multiply each by negative one, moving the entity in the reverse direction it's looking.
    • And left and right are the trickiest. Without explaining the math, you need to override the x-velocity to the z-look angle vector, and the z-velocity to the x look angle vector, multiplying one or the other by negative one to determine the direction.
    • The same thing applies for jumping, you could take the spacebar as an input, and then add y velocity to the entity if they're on the ground.
  • Item use harder, I can't think of particularly good ways to do this without essentially programming in a use case for every single item. You could probably take the mouse buttons as an input, and could reverse engineer some stuff, (like placing a block at the entity's look-position when the button is clicked), but I don't know where you'd even start with stuff like attacking.
  • The inventory is also relatively straightforward, entities can have custom inventories, and the game annoyingly creates GUIs for this inventories by default. If you wanted your custom entity's inventory to be inaccessible, that would actually be more difficult.
  • The other difficult bit is going to be getting the camera to work. The only way I can think of to do this would be to force set the player's gamemode to spectator, and then set their position every tick relative to the entity they're controlling, or set their spectating target to that entity, though both options could cause numerous problems, especially if you want this to be survival friendly. There might be plugins that could offer help with camera control, but I'd definitly be cautious attempting this in Mcreator.
Joined Mar 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hi! I read all the comment…
Mon, 03/04/2024 - 11:10

Hi! I read all the comment but I didn't understand it so well, because my english is not the best( Im from Spain). Could you make the procedure visually pls. Thank you so much ^^