- Home /
The question is answered, right answer was accepted
damage direction
how can i make damage direction like this if get hit from front show image1,if hit right show image2?
public Image direction;
void update(){
Vector3 targetPos = target.transform.position;
Vector3 screenpos = Camera.main.WorldToScreenPoint(targetPos);
...
}
How are you "hitting" something? Raycasts? Collisions? Tell me what a "hit" is and I can tell you how you can tell what direction it came from
with collision.only show Four directions image. like this link text
If I understood it right, you want to show a certain Image if the hit came from something in front of you that is being shown on camera and another image if the hit came from somewhere not shown on your camera. Is this right?
Answer by bdubbert · Oct 29, 2021 at 08:08 PM
First find the point of contact - for example
Raycast:
if(Physics.Raycast(..., out hit, ...){hitPoint = hit.Point;}
Collision
void OnCollisionEnter(Collision col){ hitPoint = col.GetContact(0); }
Then find the direction between your player and the hit point
Vector3 direction = hitPoint - player.transform.position;
Then figure out which direction most matches the direction to your hit point
float frontDot = Vector3.Dot(direction, player.transform.forward);
float rightDot = Vector3.Dot(direction, player.transform.right);
if(Mathf.Abs(frontDot ) > Mathf.Abs(rightDot )){
if(frontDot > 0) ShowFrontCollisionImage();
else ShowBackCollisionImage();
} else{
if (rightDot > 0) ShowRightCollisionImage();
else ShowLeftCollisionImage();
}
Then just right up the code to show the correct image from those functions
Follow this Question
Related Questions
Angle between 2 GameObjects and center of screen 2 Answers
Is it possible to keep the same Vector3 value using ScreenToWorldPoint with the camera moving? 0 Answers
how to drop an object when the object is set in Camera.main.ScreenToWorldPoint 0 Answers
Slow WorldToScreenPoint () 0 Answers
how to find forwards,right,up component of a velocity 2 Answers