#Projectile warning indicator UI
1 messages · Page 1 of 1 (latest)
Hey Nax, appreciate your feedaback mate. So I'm following this tutorial: https://www.youtube.com/watch?v=ewfI0il_L54&ab_channel=VainiusGames
In this video i will show you how important math is in programming and what you can make with it
You can also join my discord server:
https://discord.gg/EbMskgU
Or you can join my GameDev facebook group called YoungGameDev:
https://www.facebook.com/groups/47334...
Figured it'd be easier to do it here. You'll want to use the above method to convert the positions of both into screen space then subtract one from the other to get the difference.
And then what I'm thinking about doing is just adding a lookat function to try and make is float in the appropriate direction
I believe lookat rotates the transform such that the object's forward vector points toward a given target
I will need distance later for a different functionality but not sure if it's the one I'm looking for in this specific use case.
So is that whatI'm lookng for or?
It's not an intuitive way to do it
Okay
Distance in this case will just allow you to configure how far from the player the warning indicator shows
Okay, what I'm trying to achieve atm is to simply make the alert float on a set circular axis, but in the direction from where the projectile is coming from.
So if it's coming from straight ahead, the alert would be at the top of the circle, if from behind then it would be at the bottom and so on
And it would update as the player moves around
You'll need a list of every projectile in the scene,
Each frame iterate over them and check whether they're close enough to trigger the indicator.
If it is, convert the object position of both the player and the projectile to screenspace using the above method. Subtract the position of the object from the projectile to get the difference. Normalise the difference vector and then multiply it by some value to set the distance the indicator appears from the player. Add this new vector to the screenspace position of the player and set that as the position for your indicator UI element
Okay, looking into it now
/* References */
// List of projectile transforms in the scene
public List<Transform> projectiles;
// The camera in use
public Camera camera;
// A dictionary binding transforms to indicator instances
public Dictionary<Transform, GameObject> indicators;
public Canvas uiCanvas;
/* Config */
// Indicator distance from player in pixels
public float indicatorDistance;
// Controls how close in pixels the projectile needs to be before the indicator appears
public float indicatorThreshold;
public GameObject indicatorPrefab;
void FixedUpdate() {
var playerPosition = camera.WorldToScreenPoint(transform.position);
for (int i = 0; i < projectiles.Count; i++) {
// Get the difference between the two in screen pixels
var difference = camera.WorldToScreenPoint(projectiles[i].position) - playerPosition;
// If the difference is greater than the threshold, skip this loop
if (difference > indicatorThreshold) continue;
// Direction from the player to the projectile
var direction = difference.Normalized;
// Indicator UI position in screen space
var indicatorPosition = direction * indicatorDistance + playerPosition;
// Initialise an indicator variable
GameObject indicator;
// Try to get the existing indicator for that projectile,
// if it doesn't exist make it and add it to the dictionary
if (!indicators.TryGetValue(projectiles[i], out indicator)) {
var indicator = Instance(indicatorPrefab);
indicators.Add(projectiles[i], indicator);
}
// Set the indicator's position
indicator.position = indicatorPosition;
}
}```
Here's a very rough implementation which I haven't tested, but hopefully it will give you a basis to work off of
This will leak gameobjects though and I'm not at my PC to write a full implementation
but to do that, just kill any gameobjects whose keys are null
whoa, I've been making slow progress. Will read through the code you provided, thank you so much
this is super advanced stuff lol xD
Okay, one thing I should probably note is that I have a trigger attached to the projectile that instantiates the ui alert when triggered by the player.
And the ui alert will be destroyed after it exits a spherical trigger around the player
(not sure if working with triggers is not recommended)
That's a good idea
Okay, I've been able to get the alert moving just not in the way it's supposed to. First it follows above the object (flies above it), then it goes past the camera view, then reappears sliding from the top part of the screen and settles in its preset location.
neat, can you get a recording?
ah that makes sense, I thought this was 2d
Sorry I should have been more explicit that it was a 3d game. Any feedback on how I can fix it?
// Get the difference between the two in screen pixels
var difference = camera.WorldToScreenPoint(gameObject.transform.position) - playerPosition;
Debug.Log("difference between player and projectile is " + playerPosition.x + " pixels from the left");
// Direction from the player to the projectile
//var direction = difference.Normalized;
var indicatorPosition = difference * indicatorDistance + playerPosition;
//uiObject.transform.Translate(indicatorPosition);
uiObject.transform.position = indicatorPosition;```
Yo, just tried your code but I'm receiving a few errors
lol I did write it without any error checking. What does it throw?
// Get the difference between the two in screen pixels
var difference = camera.WorldToScreenPoint(playerPosition) - gameObject.transform.position;
Debug.Log("difference between player and projectile is " + playerPosition.x + " pixels from the left");
// Direction from the player to the projectile
//var direction = difference.Normalized;
var indicatorPosition = difference * indicatorDistance + playerPosition;
//uiObject.transform.Translate(indicatorPosition);
uiObject.transform.position = indicatorPosition;```
Got this code on projectiles hitting flying toward my character
The canvas is also on the projectile
This is what I'm trying to achieve:
Instead nothing is appearing on screen but the letter is flying on the z axis
This is what happened before
sure, I'll be home in about eight hours
Awesome