Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by drex150 · Aug 02, 2014 at 09:17 PM · enemyangleshotat

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image screenname_taken · Aug 02, 2014 at 09:22 PM 0
Share

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?

avatar image drex150 · Aug 02, 2014 at 09:30 PM 0
Share

@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.

avatar image AlucardJay · Aug 02, 2014 at 09:41 PM 1
Share

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 );

avatar image drex150 · Aug 02, 2014 at 09:57 PM 0
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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!)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image drex150 · Aug 03, 2014 at 02:23 AM 0
Share

@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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2d count collisions ontriggerenter? 1 Answer

How to make an enemy to deviate from shots? 3 Answers

Is there a way to make your robots from the FPS tutorial fire upwards and downwards? 0 Answers

If Player Looks At Enemy Script 4 Answers

Enemy being rude and not facing me 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges