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

Started by ZenZen on

Topic category: Help with modding (Java Edition)

Last seen on 01:17, 31. May 2023
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
Last seen on 04:04, 3. Feb 2023
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:

Last seen on 01:17, 31. May 2023
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!

Last seen on 04:04, 3. Feb 2023
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) )

Last seen on 04:04, 3. Feb 2023
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

Last seen on 01:05, 16. Jul 2023
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:

Last seen on 01:05, 16. Jul 2023
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.

Last seen on 08:31, 23. Dec 2023
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

Last seen on 08:31, 23. Dec 2023
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

Last seen on 08:31, 23. Dec 2023
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!!!

Last seen on 04:54, 29. Mar 2024
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?