Started by
ZenZen
on
Topic category: Help with Minecraft modding (Java Edition)
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
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:
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!
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) )
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
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:
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.
give me the finished procedure .ptpl!!!!!!!!!!!!!!!!!! Please
Try this plugin out
https://mcreator.net/plugin/95170/procedures-plus-forge-1165-1182-1192
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
Hazzah_ , Thanks for this plugin! I will check if it will be useful but I think it will be useful thanks!!!
I'm guessing this is how you made the worm?
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.