This site requires JavaScript, please enable it in your browser!
Greenfoot back
84mu3lC3p4C7
84mu3lC3p4C7 wrote ...

2022/12/12

Adding gun to a player

84mu3lC3p4C7 84mu3lC3p4C7

2022/12/12

#
Hey, I am not sure if I can explain my problem properly, but I will try my best. So I have 2 actors named Weapon and Player, my Player actor works as I want to: moves by WSAD and rotates towards my mouse. And now the Weapon actor: rotates towards mouse and follows Player coordinates, but I want it to have little offset from the Player, so it looks like the Player is holding it in right hand, but when I rotate the Player, the offset makes it look weird. I am really sorry, that I can´t explain it more proffesionally, hope you understand me, if anything is unclear, just ask me, I really appreciate your help. THX :D Player code:
public class Player extends Actor
{
    static int mouseDirection;
    static int playerX;
    static int playerY;

    public void act()
    {
        MyWorld.bulletSpawn++;
        playerX = getX();
        playerY = getY();
        MouseInfo mouseposition = Greenfoot.getMouseInfo();
        if (mouseposition != null) {
          turnTowards(mouseposition.getX(), mouseposition.getY());
          Player.mouseDirection = getRotation();
        }
        if(Greenfoot.isKeyDown("w")) {
            setLocation(getX(),getY() - 2);
        }
        if(Greenfoot.isKeyDown("s")) {
            setLocation(getX(),getY() + 2);
        }
        if(Greenfoot.isKeyDown("a")) {
            setLocation(getX() - 2,getY());
        }
        if(Greenfoot.isKeyDown("d")) {
            setLocation(getX() + 2,getY());
        }
        if(Greenfoot.isKeyDown("space") & MyWorld.bulletSpawn > 20) {
            MyWorld.bulletSpawn = 0;
            getWorld().addObject(new Bullet(), getX(), getY());
        }
    }
}
Weapon code:
public class Weapon extends Actor
{

    public void act()
    {
        setRotation(Player.mouseDirection);
        setLocation(Player.playerX + 15, Player.playerY + 10);
    }
}
Jestry Jestry

2022/12/12

#
Are the images both the Weapon and Player facing in the same direction naturally (without a rotate)?
danpost danpost

2022/12/13

#
84mu3lC3p4C7 wrote...
I have 2 actors named Weapon and Player, my Player actor works as I want to: moves by WSAD and rotates towards my mouse. And now the Weapon actor: rotates towards mouse and follows Player coordinates, but I want it to have little offset from the Player, so it looks like the Player is holding it in right hand, but when I rotate the Player, the offset makes it look weird. << Code Omitted >>
Do not put movement/rotation code in the Weapon class. It is the player that holds/controls the position/rotation of the weapon. So, have that code in the Player class. Remove static from the fields in the Player class. Add a field to contain the Weapon object being held by the player. so the weapon can be easily controlled.
84mu3lC3p4C7 84mu3lC3p4C7

2022/12/13

#
Jestry wrote...
Are the images both the Weapon and Player facing in the same direction naturally (without a rotate)?
Yes, they both are facing to the right (0°).
84mu3lC3p4C7 84mu3lC3p4C7

2022/12/15

#
danpost wrote...
Add a field to contain the Weapon object being held by the player. so the weapon can be easily controlled.
That´s the problem, I´am not sure how to do that, if there is any function that glues it to the Player? I´ve tried to draw my own image of gun and leave there a blank place, where the player would be, because the "setLocation(playerX, playerY);" in Weapon class centers it to Player, but then other problems came: hitbox was too big and spawning the bullets didn´t wark as I wanted to. This is how I want it to look while facing any direction (not just the 0°), with both objects being ratated to mouse: This is how I don´t want it to look like:
84mu3lC3p4C7 84mu3lC3p4C7

2022/12/15

#
I wasn´t able to post messages, sorry I wasn´t responding. I got little bit closer to what I want by looking at this: How to rotate an object orbiting another object without making the object rotate, so thank you danpost.
public class Weapon extends Actor
{
    private int distance;
    private int centerX, centerY;
    private int angle;

    public void act()
    {
        distance = 10;
        centerX = Player.playerX;
        centerY = Player.playerY;
        angle = Player.mouseDirection;
        setLocation(centerX, centerY);
        setRotation(angle+1);
        angle = getRotation();
        move(distance);
    }
There is only one small detail missing and that is that I want the Weapon to be just a bit to the right, not just directly in front of the Player. Edit: Now I have what I wanted:
public void act()
    {
        setLocation(Player.playerX, Player.playerY);
        setRotation(Player.mouseDirection-90);
        move(5);
        setRotation(Player.mouseDirection+90);
        move(10);
        }
    }
It just makes the Weapon a bit laggy, any solution for this?
You need to login to post a reply.