0
\$\begingroup\$

I'm trying to rotate my camera 'next to' the player object so that the player is not in the way of the screen-centered reticle. What I am trying to achieve is something like the targetting system of Fortnite, GTA 5 or Red Dead Redemption (or most third-person shooters), where the camera/player are offset slightly so that you can see directly in front of them.

My code so far allows me to rotate the camera about the player (object here) and it follows the player's position. I cannot figure out how to offset it.

public void update(final GameObject object)
    {
        this.newMouseX = Input.getMouseX();
        this.newMouseY = Input.getMouseY();

        final float dx = (float) (this.newMouseX - this.oldMouseX);
        final float dy = (float) (this.newMouseY - this.oldMouseY);

        // Rotate the camera on mouse move
        this.verticalAngle -= dy * this.mouseSensitivity;
        this.horizontalAngle += dx * this.mouseSensitivity;

        // Distances from side view
        final float horizontalDistance = (float) (this.distance * Math.cos(Math.toRadians(this.verticalAngle)));
        final float verticalDistance = (float) (this.distance * Math.sin(Math.toRadians(this.verticalAngle)));

        // Distances from top view
        final float xOffset = (float) (horizontalDistance * Math.sin(Math.toRadians(-this.horizontalAngle)));
        final float zOffset = (float) (horizontalDistance * Math.cos(Math.toRadians(-this.horizontalAngle)));

        this.position.set(object.getPosition().getX() + xOffset, object.getPosition().getY() - verticalDistance + VERT_OFFSET, object.getPosition().getZ() + zOffset);

        this.rotation.set(-this.verticalAngle, this.horizontalAngle, 0);

        this.oldMouseX = this.newMouseX;
        this.oldMouseY = this.newMouseY;
    }

enter image description here

This is the sort of thing I'm trying to achieve: enter image description here

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

Your character object should have vectors representing its facing (forward, right, up). If you take the normalized right vector and multiply it by a scaling factor, you will create an offset vector. Add the offset to the player position and store it in a separate variable. This is where your camera will rotate around, roughly speaking. Don't forget to update the offset each frame.

\$\endgroup\$
0
\$\begingroup\$

first thing, I didn't made any game in third person view yet. But as of camera follow it should be easy. What I do is:-

I creates a public vector 3 variable named offset and use that as it:-

void Update()
{
   transform.position = player.position; /* this script should be in camera
   object and player is a transform variable which you can assign */
   transform.position += offset;
}

then I will go in game view and change the value of offset to put it as of third person view. Then I will copy the values or remember them, will exit from game view and set that in the offset variable. This is what I will do, I hope it also works for you. You can use any function for moving camera just remember the logic behind it.

\$\endgroup\$
0
\$\begingroup\$

Thanks for the responses, but I have solved it myself after multiple attempts at the maths. I had to modify xOffset and zOffset with the following:

final float D = 20f;
final float d = 2f;
final float xOffset = (float) ((d * Math.cos(Math.toRadians(-this.horizontalAngle))) + (D * Math.cos(Math.toRadians(90 - -this.horizontalAngle))));
final float zOffset = (float) ((D * Math.sin(Math.toRadians(90 - -this.horizontalAngle))) - (d * Math.sin(Math.toRadians(-this.horizontalAngle))));
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .