Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by phato777 · Dec 22, 2015 at 07:21 PM · uienemydirectiondamage

Damage indicator representing incoming damage direction with arrow in UI

I've been banging my head over this. I'm terrible with rotations, especially when it comes to translating them from and to 2D space.

So what I have is a 96px square sprite that has it's pivot set to 0.5 and 0 (bottom center) at the center of the screen and the default arrow direction is 0 degrees (pointing up). When the player takes damage, I'm trying to make this arrow rotate around the pivot (center of the screen) and show which direction the damage came from. So if the damage came from behind them, the arrow would be at 180 degrees (pointing down), in front 0 degrees (pointing up), to the left 270 degrees (pointing left)... Well, you get the idea.

Here is what I got, but it just doesn't seem to work right:

     Vector3 targetPos = target.transform.position;

     Vector3 screenpos = Camera.main.WorldToScreenPoint(targetPos);

     if(screenpos.z < 0)
         screenpos *= -1;

     float angle = Mathf.Atan2(screenpos.y, screenpos.x);
     angle -= 90 * Mathf.Deg2Rad;

     ArrowSprite.transform.localRotation = Quaternion.Euler(0, 0, angle * Mathf.Rad2Deg);

I found some code somewhere and this is the result I came up with while tweaking it trying to make it work as I don't quite understand what the -= 90 is for. I do understand taking Atan2 and turning it into a direction and then back to radians for the Quaterniun.Euler, but I'm clearly not understanding the full logic of this code or I'd be able to get my arrow to point in the correct direction in relation to the incoming damage.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by G4merSylver · Dec 22, 2015 at 07:48 PM

I had created my damage indicator via attaching an tag to the object that would damage, then created an script that relying on vector3.distance told me which one was the closest and then I used transform.lookAt to lock on the damage, it would be helpful if you attach the lookAt script on an empty gameobject which holds the arrow thats a child of it and has a slight offset. Using vector3.distance also allows you to limit the range :)

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 phato777 · Dec 22, 2015 at 08:02 PM 0
Share

Thanks for the reply. $$anonymous$$y arrow is underneath a canvas. I have the damage inflictors position. I basically just need to represent in 2D space the direction of that inflictor. I'd like to keep the setup I have, I like having all my UI stuff underneath the canvas.

As for distance, I don't really need to worry about that, just a simple arrow pointing to the damage inflictor so the player knows which direction to turn to face their enemy.

avatar image
0

Answer by RenanUnityBrasil · Apr 04, 2017 at 04:41 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class hitDirection : MonoBehaviour {
     public Transform target;
     public Image direction;
     void Update()
     {
         Vector3 relativePos = target.position - transform.position;
         Quaternion rotation = Quaternion.LookRotation(relativePos);
         print(rotation.eulerAngles) ;
 
         direction.rectTransform.localRotation = Quaternion.Euler(0,0,-rotation.eulerAngles.y);
     }
 }


Comment
Add comment · Show 2 · 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 FElineRAptor7273 · Nov 15, 2021 at 04:27 AM 0
Share

but what if i have multiple targets/enemies? like for eg. i have 16 enemies in my scene that shoot at me

avatar image FElineRAptor7273 · Nov 15, 2021 at 07:23 AM 0
Share

and this does not work

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

43 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 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

How can I get my enemy to hurt/kill player 0 Answers

Enemy Isn't Hurting Player in Survival Shooter 0 Answers

My enemy don't take damage. 1 Answer

How do I make this equal to a Transform? 1 Answer

2D How to get enemy to face the player while moving 0 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