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.
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 :)
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.
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);
}
}
but what if i have multiple targets/enemies? like for eg. i have 16 enemies in my scene that shoot at me
Your answer
Follow this Question
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