Any way to force a mob to look at player using procedures?

Started by ZenZen on

Topic category: Help with Minecraft modding (Java Edition)

Joined Oct 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Any way to force a mob to look at player using procedures?
Mon, 12/26/2022 - 11:02 (edited)

Hello, am trying to find a way to force my mob to look at the player using procedures. I've had little success the only thing working for me so far is commands but those aren't perfect and can mess with the TPS a bit. Does anyone especially if your good at trigonometry know how to do this?

Edited by ZenZen on Mon, 12/26/2022 - 11:02
Joined Dec 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I was unable to get this…
Tue, 12/27/2022 - 13:40

I was unable to get this working properly with procedures alone, but was able to make it work with custom code.

I'm still learning how this stuff works myself, so definitely experiment on your own and perhaps you'll get better results, but what was required on my side was a combination of the following:

setDeltaMovement
moveControl.setWantedPosition
lookControl.setLookAt

In that order.

 

As far as the trig side of things go, the angle between two things can be calculated using atan2.
Specifically, you want the inputs to be atan2(z, x), which looks a little wacky in the MCreator interface (put Z on the left).

I come from a background where atan2 takes inputs of (y, x) so this is especially weird in a game where Y is up/down haha

In an example where we want the mob to look at the player, the logic would look something like this:
local playerPosition
local mobPosition
local deltaPosition

set deltaPosition x = playerPosition x - mobPosition x
(repeat for y and z)

angle from mob to player = atan2 using deltaZ and deltaX

 

It looks like the lookController for each entity has the ability to set a desired target entity instead of a particular desired position or angle, which you might find more useful. A snippet to get the player as a target to try with this looks like this:

Joined Oct 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I see very interesting. I…
Thu, 12/29/2022 - 07:38

I see very interesting. I did sorta find a way to do this through procedure as pictured below

Still trying to figure out how to do the head pitch and changing the turn speed. But thanks for the reply the info was helpful!

Joined Dec 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
For pitch try negative ASin…
Thu, 12/29/2022 - 15:35

For pitch try negative ASin of the normalized direction's Y

To normalize a direction, first get the delta (location target - my location)

the normal is sqrt( (deltaX * deltaX) + (deltaY * deltaY) + (deltaZ * deltaZ) )

Joined Dec 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Sorry, meant to say that the…
Thu, 12/29/2022 - 23:25

Sorry, meant to say that the x y and z values of the delta are then multiplied by that square root value to get the normalized direction

Joined Jun 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This thread helped me solve…
Fri, 06/30/2023 - 23:07

This thread helped me solve my own problem, so I thought I'd add some info

You actually divide dY by the normal. So to get pitch, your procedure blocks look like this:

Joined Jun 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Images. How do they work? Oh…
Fri, 06/30/2023 - 23:14

Images. How do they work? Oh well

In text:

dX = (player X - entity X);

dY = (player Y - entity Y);

dZ = (player Z - entity Z);

 

N = sqrt(dX^2 + dY^2 + dZ^2);

pitch = -1 * asin (dY / N);

Use Pitch when you want to aim vertically at the player. I used this in a "shoot from" procedure block where it asks for direction dY and now my mob actually shoots down or up at the player.

Joined Jul 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
give me the finished…
Mon, 07/31/2023 - 12:05

give me the finished procedure .ptpl!!!!!!!!!!!!!!!!!! Please

Joined Jul 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Sorry, I already figured it…
Tue, 08/01/2023 - 05:07

Sorry, I already figured it out myself and made the procedure from the screen. But still, give it ready, I'll check if the topic is still alive this year))) To know if the forum is working

Joined Jul 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hazzah_ , Thanks for this…
Tue, 08/01/2023 - 05:10

Hazzah_ , Thanks for this plugin! I will check if it will be useful but I think it will be useful thanks!!!

Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I'm guessing this is how you…
Thu, 08/03/2023 - 20:00

I'm guessing this is how you made the worm?

Joined Jun 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Here's how I did mine if…
Tue, 08/20/2024 - 06:05

Here's how I did mine if anyone else just wants to copy it without learning trig lol

https://imgur.com/gallery/force-mob-to-look-entity-every-tick-yaw-pitch-no-jitter-TFM49Cf
The Potion effect is to make the mobs rotation smooth I'm just using it as a timer btw if so if you want the mob to look at you every 5 ticks you can set the potion effect to 5 ticks or if you only plan on firing it once you can simply remove it try to use a potion effect that you think will never be applied naturally to the mob or it will effect the timing you can also make a custom one if you think it will be a problem.