Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I'm trying to figure out a way to force a player to look at a nearby entity. I know that this can be done the opposite way (forcing an entity to look at a player) using:

<entity>.getLookHelper().setLookPosition(<player>.posX, <player>.posY + (double)<player>.getEyeHeight(), <player>.posZ, 10.0F, (float)<entity>.getVerticalFaceSpeed());

But seeing as a player entity doesn't have the .getLookHelper() method, I'm not sure how to do this a similar way. I understand that a player's position and rotation can be set with methods like .setPositionAndRotation(x, y, z, yaw, pitch), where yaw and pitch would determine rotation, but I'm not sure how to find/calculate what yaw and pitch to use to direct the player to look at the entity.

It involves lots of trigonometry, but basically you subtract the target's position from the player's position on both the horizontal axes and run that through atan to get the angle, then subtract the player's current rotation yaw from that angle and make sure it's clamped between -180 and 180. I found I had to add 90 to it after the fact, probably as the result of some quirk in Minecraft's coordinate system.

 

You can use similar math for the vertical axis, but be sure to include the entities' respective heights and/or eye positions in your calculations.

 

Once you have both angles, call player#setAngles(yaw, pitch) and you're good to go. Perhaps consider setting the player's head yaw instead of their entire body, depending on your situation. I do this on the client side, btw, but you may want to do the calculations on both sides.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.