Jump to content

Changing the players camera angle/looking direction


chimera27

Recommended Posts

I'm trying to change the players looking direction to face an entity, but don't know how. I have code to get the entity the player is looking at, and did the trig to get the angles to set it to, but can't figure out how to set it.

Here's my code for handling this kind of thing (run every tick, probs because i'm stupid and don't know a better way :P)

 

 

ExtendedPlayer props = ExtendedPlayer.get(player);

 

if (Keyboard.isKeyDown(29)) {

System.out.println("Player holding control");

target = props.getGetTarget();

if (target != null) {

System.out.println("Locked on to: " + target);

//I tried to set it here by just setting these to the trig functions, no luck :/

player.cameraYaw = (float) Math.sin((target.posX - player.posX)

/ (target.posZ - player.posZ));

player.cameraPitch = (float) Math

.sin((target.posX - player.posX)

/ (target.posY - player.posY));

 

} else {

float X = (float) Math.sin(90 - player.cameraPitch) * 10;

float Y = (float) Math.sin(player.cameraPitch) * 10;

float Z = (float) Math.sin(player.cameraYaw) * 10;

 

if (Minecraft.getMinecraft().objectMouseOver.entityHit != null) {

target = Minecraft.getMinecraft().objectMouseOver.entityHit;

System.out.println("Target is: " + target);

if (target instanceof EntityLiving) {

props.setTarget((EntityLiving) target);

}

}

 

}

}

props.setTarget(null);

target = null;

 

 

Anyone know a good way to do this?

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Yes. First, use the RenderTick so that your player can change direction smoothly, then use the player.rotationYaw/Pitch, NOT the camera yaw/pitch. The trick here is you don't actually need to change the camera, you merely need to change the player's rotation - change the direction the player faces, and the camera will be where you want it ;)

 

Finally, I would suggest calculating the difference from the player's current yaw/pitch to the new yaw/pitch, and only adding part of that at a time to avoid the player's view changing suddenly - it can be quite a jarring experience.

Link to comment
Share on other sites

Still not working :/ Also, how do I get the player from RenderTickEvent? Is there a player render tick or something of the sort?

Rendering is client side, so Minecraft.getMinecraft().thePlayer is an acceptable way to get the player, though you may need to check if it's not null (see if you crash or not :P )

 

Rather than getting Minecraft every render tick, you can set it during your render tick listener class constructor (though you should only listen to client-side events in this class)

 

 

public class TargetingTickHandler
{
private final Minecraft mc;

public TargetingTickHandler() {
this.mc = Minecraft.getMinecraft();
}

@SubscribeEvent
public void onRenderTick(RenderTickEvent event) {
if (event.phase == Phase.START) {
// player available is: mc.thePlayer, need to null check if you crash during world start-up
// update player rotation (i.e. render view) based on your conditions
}
}

 

Link to comment
Share on other sites

  • 4 years later...

coolAlias: I am sorry for my necromancy here, but I'd like to ask related question. You said:

 

Quote

Yes. First, use the RenderTick so that your player can change direction smoothly, then use the player.rotationYaw/Pitch, NOT the camera yaw/pitch.

Note, the emphasizes is mine.

What do you exactly mean by changing direction smoothly? Is there a way to change direction smoothly in a sense that player will get more than 20 FPS when rotating? I already tried hooking my yaw rotation changing code onto drawScreen() method and onRenderTick() methods and I never get the smooth feeling out of it. It is always capped at 20 changes per second (so... it updates every tick, which makes the transition 20 FPS, despite the rest of  the world runs nicely 60fps). Any advice here?

Edited by DeadPix
Updated wording.
Link to comment
Share on other sites

  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • If you're seeking an unparalleled solution to recover lost or stolen cryptocurrency, let me introduce you to GearHead Engineers Solutions. Their exceptional team of cybersecurity experts doesn't just restore your funds; they restore your peace of mind. With a blend of cutting-edge technology and unparalleled expertise, GearHead Engineers swiftly navigates the intricate web of the digital underworld to reclaim what's rightfully yours. In your moment of distress, they become your steadfast allies, guiding you through the intricate process of recovery with transparency, trustworthiness, and unwavering professionalism. Their team of seasoned web developers and cyber specialists possesses the acumen to dissect the most sophisticated schemes, leaving no stone unturned in their quest for justice. They don't just stop at recovering your assets; they go the extra mile to identify and track down the perpetrators, ensuring they face the consequences of their deceitful actions. What sets  GearHead Engineers apart is not just their technical prowess, but their unwavering commitment to their clients. From the moment you reach out to them, you're met with compassion, understanding, and a resolute determination to right the wrongs inflicted upon you. It's not just about reclaiming lost funds; it's about restoring faith in the digital landscape and empowering individuals to reclaim control over their financial futures. If you find yourself ensnared in the clutches of cybercrime, don't despair. Reach out to GearHead Engineers and let them weave their magic. With their expertise by your side, you can turn the tide against adversity and emerge stronger than ever before. In the realm of cybersecurity, GearHead Engineers reigns supreme. Don't just take my word for it—experience their unparalleled excellence for yourself. Your journey to recovery starts here.
    • Ok so this specific code freezes the game on world creation. This is what gets me so confused, i get that it might not be the best thing, but is it really so generation heavy?
    • Wizard web recovery has exhibited unparalleled strength in the realm of recovery. They stand out as the premier team to collaborate with if you encounter withdrawal difficulties from the platform where you’ve invested. Recently, I engaged with them to recover over a million dollars trapped in an investment platform I’d been involved with for months. I furnished their team with every detail of the investment, including accounts, names, and wallet addresses to which I sent the funds. This decision proved to be the best I’ve made, especially after realizing the company had scammed me.   Wizard web recovery ensures exemplary service delivery and ensures the perpetrators face justice. They employ advanced techniques to ensure you regain access to your funds. Understandably, many individuals who have fallen victim to investment scams may still regret engaging in online services again due to the trauma of being scammed. However, I implore you to take action. Seek assistance from Wizard Web Recovery today and witness their remarkable capabilities. I am grateful that I resisted their enticements, and despite the time it took me to discover Wizard web recovery, they ultimately fulfilled my primary objective. Without wizard web recovery intervention, I would have remained despondent and perplexed indefinitely.
    • I've tested the same code on three different envionrments (Desktop win10, desktop Linux and Laptop Linux) and it kinda blows up all the same. Gonna try this code and see if i can tune it
    • Minecraft version? Mod loader?
  • Topics

×
×
  • Create New...

Important Information

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