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



×
×
  • Create New...

Important Information

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