- Home /
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: https://www.youtube.com/watch?v=0oDOf1gniso&list=UUhMjr9l80eeX4lLNU6Lry3A#t=172
Since you already know the gameobject, you also know it's transformation. How about just using Transform.LookAt to the arrow that'll show you where you are being shot from?
@screenname_taken I've considered the idea but I want it to look more like this video. Watch in about 2 seconds the red indicator will show up and float around the screen.: https://www.youtube.com/watch?v=0oDOf1gniso&list=UUh$$anonymous$$jr9l80eeX4lLNU6Lry3A#t=172
I think using Transform.LookAt would only work if I was using some kind of 3D object ins$$anonymous$$d of a 2D GUI type object. If I was using a 3D arrow or something that would work though.
$$anonymous$$aybe I'm wrong, but I'm assu$$anonymous$$g that in 2D space, Transform.LookAt isn't going to want to work.
off the top of my head, something like :
Vector3 shooterDir = shooter.position - player.position;
shooterDir.y = 0;
Quaternion shooterRot = Quaternion.LookRotation( shooterDir );
Debug.Log( shooterRot.EulerAngles.y );
@alucardj That is actually very close. The only problem is it's not taking in to account the direction the player that is being shot is facing. It is giving me a 0-360 which is awesome, but it's based entirely on position so if the killer shoots him and returns 0 for the angle, if the player turns 180 degrees and the killer shoots him again without moving, it should return 180 this time.
Sorry if I'm not being clear enough, it's kind of hard to describe exactly what I'm going for haha. Thanks for the help though, this seems to be very close. ^^
I thought I had a solution for a second but I was wrong. Still need to figure out how to take the player's forward in to account.
Answer by AlucardJay · Aug 02, 2014 at 11:20 PM
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 :
http://docs.unity3d.com/ScriptReference/Vector3.Dot.html
http://docs.unity3d.com/ScriptReference/Vector3-normalized.html
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!)
@alucardj Awesome, this works perfectly! Thanks a lot hehe. I actually have a request of you. I am working on a chat system, and if you've got free time and wouldn't $$anonymous$$d looking at this one, that'd be awesome. No worries if you can't. Seems to have stumped people though, not getting much help on it. Thanks again! ^^
http://answers.unity3d.com/questions/761725/chat-script-works-in-editor-but-not-in-stand-alone.html