Player Zoom In

Discussion in 'Resources' started by Phinary, Jan 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Phinary

    Hey guys, I haven't seen this get used before so I thought it would be cool to post this. Note that this is probably completely useless as it can be very glitchy, but I found it pretty fun to fool around with.
    You may be able to use this with a gun plugin and make sniper rifles or something similar :)

    Anyways, I think the code will speak for itself, I will explain it a bit after. Basically this abuses what I think is a bug when you adjust player speed you can get some interesting FOV changes and other interesting effects.

    Interact Event:
    Code:
        public void onPlayerInteract(PlayerInteractEvent event) {
            if (event.getPlayer().getItemInHand().getType() == Material.DIAMOND_SPADE) {
                KitsPlayer kplayer = PlayerHandler.getPlayer(event.getPlayer().getName());
                if (kplayer == null) return;
           
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    //zoom in
                    kplayer.toggleZoom();
                } else if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
                    if (kplayer.isZoomedIn()) {
                        Snowball snowball = event.getPlayer().launchProjectile(Snowball.class);
                        snowball.setShooter(event.getPlayer());
                        snowball.setVelocity(snowball.getVelocity().multiply(3));
                    } else {
                        event.getPlayer().sendMessage("You must scope in to shoot!");
                    }
               
                }
           
            }
        }
    Move Event:
    Code:
        public void onPlayerMove(PlayerMoveEvent event) {
            KitsPlayer kplayer = PlayerHandler.getPlayer(event.getPlayer().getName());
            if (event.getTo().getBlockX() == event.getFrom().getBlockX() && event.getTo().getBlockY() == event.getFrom().getBlockY() && event.getTo().getBlockZ() == event.getFrom().getBlockZ()) return;
     
            //again, you can ignore the kitsplayer stuff, that is just a class for storing player data
            if (kplayer != null) {
                if (kplayer.isZoomedIn()) {
                    kplayer.toggleZoom();
                    event.getPlayer().sendMessage("Your motion has stopped you from aiming down sight!");
                }
            }
        }

    toggleZoom() and isZoomedIn()
    This should be per player, could use a hashmap or create instances of a class for players
    Code:
        public void toggleZoom() {
            Player player = Bukkit.getServer().getPlayerExact(playername);
            if (zoom) {
                if (player != null) {
                    //resets the walking speed to normal
                    player.setWalkSpeed(0.2F);
                }
            } else {
                if (player != null) {
                    //sets the walk speed to -0.15, this adds a zoomed in effect
                    player.setWalkSpeed(-0.15F);
                }
            }
            zoom = !zoom;
        }
     
        public boolean isZoomedIn() {
            //zoom is just a boolean ofcourse
            return zoom;
        }

    Okay so, basically from fooling around with random things, I figured out that if you use player.setWalkSpeed() and set it to around -0.1 -> -0.2 ish, it will cause the player's camera to zoom in. The amount of zoom depends on the number. -0.2 will zoom in a lot, -0.15 quite a bit less. Also, I am not sure if 0.2 is the default walking speed, it seemed pretty close.
    With all this code together, what you get is a cool effect where if you hold a diamond shovel and you right click, your camera will zoom in like you are aiming down sight, if you left click while aiming you will shoot a snowball.


    Well, sorry if it is a bit confusing and/or useless :p I found it interesting, so you guys might as well. Sorry if something like this has been posted before as well.
     
  2. I hope you used a hashmap fot the zoom variable, else it get confilcted when there are more players zooming
     
  3. Offline

    Phinary

    "zoom" is inside my class called KitsPlayer, there is a different instance for every player. This is inside my kit plugin for our server, was just fooling around with some things.
     
  4. Offline

    TheGamersCave

    I've been fiddling with what you posted, and I've gotten it to zoom in/out based on a "pressure" variable, kind of like how the bow works, except I'm working on sword throwing :)
     
  5. Offline

    Woobie

    Or you could just add a slowness potion effect.
     
  6. Offline

    Icyene

    Yet that adds the particle effects & shows the slowness icon in inventory. His method doesn't, so its more clean from the user's perspective.
     
  7. Offline

    Woobie

    Though you can add potion effects without particles, and who really cares about the icon? I was just sayin' ;)
     
    MrBluebear3 likes this.
  8. Offline

    Icyene

    OCD, of course. :)
     
    techboy291 and MrBluebear3 like this.
  9. Offline

    Gawdzahh

    I found alot easier way instead of using HashMaps and such for storing if the player is zoomed or not...
    Code:java
    1. Player player = event.getPlayer();
    2. if(player.getWalkSpeed() == 0.2F){
    3. player.setWalkSpeed(-0.15F);
    4. }else if(player.getWalkSpeed() == -0.15F){
    5. player.setWalkSpeed(0.2F);
    6. }
     
  10. Offline

    Phinary

    That works too, although I was testing around with different settings per player and using that in different scenarios, so something like a hashmap was a better option in the case of when I was coding this.
     
Thread Status:
Not open for further replies.

Share This Page