#Projectile warning indicator UI

1 messages · Page 1 of 1 (latest)

summer zinc
prime knoll
#

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.

summer zinc
#

And then what I'm thinking about doing is just adding a lookat function to try and make is float in the appropriate direction

prime knoll
#

I believe lookat rotates the transform such that the object's forward vector points toward a given target

summer zinc
#

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?

prime knoll
#

It's not an intuitive way to do it

summer zinc
#

Okay

prime knoll
#

Distance in this case will just allow you to configure how far from the player the warning indicator shows

summer zinc
#

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

prime knoll
#

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

summer zinc
#

Okay, looking into it now

prime knoll
#
/* 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

summer zinc
#

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)

prime knoll
#

That's a good idea

summer zinc
#

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.

prime knoll
#

neat, can you get a recording?

summer zinc
#

yea

prime knoll
#

ah that makes sense, I thought this was 2d

summer zinc
#

Sorry I should have been more explicit that it was a 3d game. Any feedback on how I can fix it?

summer zinc
#


        // 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;```
summer zinc
#

Yo, just tried your code but I'm receiving a few errors

prime knoll
#

lol I did write it without any error checking. What does it throw?

summer zinc
#

there's like 3 😅

#

Mate would you be willing to get on a call with me?

summer zinc
#


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

prime knoll
summer zinc
#

Awesome