# Third Person View – Aim Direction (Solved)

I have searched but havent found a good enough solution for getting the aim direction correctly in a 3rd person shooter.

The camera is positioned above the player and its direction is used for computing the direction of the projectile, that will be shot out at the lower-positioned player.

The shooting direction is easily computed when you shoot at the floor: You cast a ray from the location of the camera following the cameras direction (== crosshair "direction). You substract the players weapon location from the contact point of the floor to get the direction from the weapons side of view.

However, If your ray doesnt intersect with anything (e.g. shooting into sky), i have to make up an infinetely far away point. How to do this? At the moment im using cam direction for the weapons direction at this moment, but this produces bad results. The shooting angle becomes even smaller when you compare shooting on a distanced surface and the sky.

[java]

private Vector3f computeDirection(Vector3f playerLoc, Vector3f camLoc, Vector3f camDir) {

Ray ray = new Ray(camLoc,camDir);

CollisionResults results = new CollisionResults();

gameState.getRootNode().collideWith(ray, results);

if (results.size()>0) {

Vector3f target = results.getClosestCollision().getContactPoint();

Vector3f direction = target.subtract(playerLoc).normalize();

return direction;

} else {

return camDir;

}

}

[/java]

was rather trivial i forgot to normalize the direction

[java]

private Vector3f computeDirection(Vector3f playerLoc, Vector3f camLoc,

Vector3f camDir) {

Ray ray = new Ray(camLoc,camDir);

CollisionResults results = new CollisionResults();

gameState.getRootNode().collideWith(ray, results);

if (results.size()>0) {

Vector3f target = results.getClosestCollision().getContactPoint();

Vector3f direction = target.subtract(playerLoc).normalize();

return direction;

} else {

Vector3f farTarget = camDir.mult(1000f);

Vector3f direction = farTarget.subtract(playerLoc).normalize();

return direction;

}

}

[/java]

1 Like

This allows you the results without using collision… you can check collision for line-of-sight only if needed. Plus, if you only care about certain axis’, you can zero out the one you don’t care about.

[java]

Vector3f tV = whatever the target vector is

Vector3f nV = whatever the origin vector is

float x = (tV.getX()-nV.getX())/FastMath.PI;

float y = (tV.getY()-nV.getY())/FastMath.PI;

float z = (tV.getZ()-nV.getZ())/FastMath.PI;

Vector3f dir = new Vector3f(x,y,z);

[/java]

I just realized you may want to do this whether or not you have a target. /shrug. Still may be useful.

1 Like

Thx, but what does this do exactly? if I have the target vector and origin Vector, the direction can be simply calculated as targetVector-originVector.

It does the same thing as targetVector-originVector. I was thinking in terms of MMO type targeting, where you have a selected target and then realized you were probably working on an FPS (or TPS it sounds like).

1 Like