How to check where I'm being shot from.

Hey there,

So I’m working on an FPS multiplayer game. When a player shoots another player, I am able to tell the player that was shot what gameObject shot them. So that’s already working. What I would like to have happen is when he is shot, an indicator shows up on the camera showing what direction he was shot from.

Basically it would pop up in the direction I should turn my camera to see him. So what I was thinking is some how getting my forward direction, the other player’s x and z position, and then some how getting an angle based on that info. So if he was directly behind me it’d read out 180, if he’s to my right it’d be 45, to my left 270, and directly in front 0 or 360.

How would I go about doing something like that? I’ve tried using Vector3.Angle on my forward direction and their position. There are two problems with that. First, the further the player is from me, the higher the number gets, second it’s taking the y axis in to account which it just simply doesn’t need to do.

Thanks.

EDIT: A bit more clarification. I want this to be a 2D object that floats around the camera much like what happens in this video: - YouTube

Another method is to use Dot Product. This will return a value between -1 and 1 based on the difference between two directional vectors. When using Dot Product, make sure the vectors used are normalized. References :

Here is something I quickly tested to show how this method works :

void Update() 
{
	// using Vector3.Dot
	// first : find out if infront or behind with forward Dot product
	// dot of forward returns value between -1 and 1
	// convert result to a value between 0 and 180
	// angle = dot + 1; -> value between 0 and 2
	// angle *= 0.5; -> value between 0 and 1
	// angle *= 180; -> value between 0 and 180
	// shortened to angle = (a + 1) * 0.5 * 180 = (a + 1) * 90;
	// second : find out if left or right of player forward
	// dot of righthand side returns value between -1 and 1
	// -1 means it is left, 1 means it is right
	// if (left is < 0), then multiply angle by -1 to give a value between -180 and 0
	// angle *= -1;
	// now the angle is a value between -180 and 180, in relation to the player forward


	// calculate forward
	Vector3 shooterDir = shooter.position - player.position;
	shooterDir.y = 0;
	shooterDir.Normalize();

	Vector3 fwd = player.forward;

	float a = Vector3.Dot( fwd, shooterDir );

	float angle = (a + 1f) * 90f;


	// calculate if left or right
	Vector3 rhs = player.right;
	
	if ( Vector3.Dot( rhs, shooterDir ) < 0 )
		angle *= -1f;
	
	
	// rotate an indicator
	Quaternion indicatorRot = Quaternion.Euler( 0, 180, angle ); // assuming indicator is child of camera, forward is facing the camera

	// rotate gui indicator
	if ( indicator )
		indicator.localRotation = indicatorRot;
}

There is probably a better solution using quaternion calculations (but I’m not very good at those!)