Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

WorldToScreenPoint help please

Discussion in 'Scripting' started by Deleted User, Jun 18, 2017.

  1. Deleted User

    Deleted User

    Guest

    The blue square indicated the location of the current lock on target when it's off screen and it works very well except for when the player runs too far to the north of the enemy. The first picture shows it working correctly, and the second is just a few feet away to the north and the blue square gets completely inverted. Any ideas? Thanks.


     
    Last edited by a moderator: Jun 18, 2017
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I'm not sure your question is clear - what is that screenshot supposed to illustrate?
     
  3. Deleted User

    Deleted User

    Guest

    I apologize. I've now updated the OP with a lot better description.
     
    Last edited by a moderator: Jun 18, 2017
  4. Deleted User

    Deleted User

    Guest

  5. Deleted User

    Deleted User

    Guest

    You ought to post some code so we can help you. I think I understand what the problem is, you want the blue man to go to that diamond/box but when the enemy moves "upwards"/"north" it goes in the opposite direction.
     
  6. Deleted User

    Deleted User

    Guest

    screenPos = cam.WorldToScreenPoint(positionOfEnemy);

    if (screenPos.y > 0)
    screenPos = new Vector3(screenBoundsY / m, screenBoundsY, 0);


    The problem is that the following returns a positive value when it should be negative: cam.WorldToScreenPoint(positionOfEnemy);

    I assume it's overflowed and the flag is changed.


    edit: it was pretty easily fixed by doing the following check:

    if (screenPos.y > 0 && (positionOfEnemy.z > cam.transform.position.z))
    screenPos = new Vector3(screenBoundsY / m, screenBoundsY, 0);

    instead of:

    if (screenPos.y > 0)
    screenPos = new Vector3(screenBoundsY / m, screenBoundsY, 0);
     
    Last edited by a moderator: Jun 19, 2017
  7. Deleted User

    Deleted User

    Guest

    So for code on any boards you ought to use the code feature. Makes things easier to read. Its on the upper bar in the text window when you post like I've done here.

    My question is do you mean to give your screenPos the same value for both X & Y? I see that on the bolded line you have given the screenPos variable screenBoundsY /m (for x) and screenBoundsY (for Y). Perhaps you meant to divide ScreenBoundsX by 'm' instead? That could be causing your problem.

    Additionally you ought to give the screenPos Vector3 a Z component value that is non-zero. It preferably ought to be the same as the distance of your canvas from your camera position, depending on camera/canvas setup.
     
    Last edited by a moderator: Jun 19, 2017
  8. Deleted User

    Deleted User

    Guest

    Ok. I'll use the code feature next time then. The main problem was that the function:
    Code (CSharp):
    1. 1.cam.WorldToScreenPoint(positionOfEnemy);
    returned a positive value instead of a negative due to the number being too large. I don't think it's intended. I only posted a snippet of the code and not the code for the screenBoundsX.

    Can you please tell me what you mean with the Z component? I've tested with different values and I don't notice any difference when changing the Z value. Very kind of you to write so much.
     
  9. Deleted User

    Deleted User

    Guest

    Vector3's have three component values, namely x,y, and z.
    So:
    Code (CSharp):
    1. Vector3 myVector3 = new Vector3(xComponent, yComponent, zComponent);
    I think I was confused on saying that it was the Z component. Even if your screen point was behind the camera, it would not change the position on it due to perspective. Just ignore what I said there...

    Sometimes I get so pumped to help that I make silly mistakes...:p
     
    Deleted User likes this.
  10. Deleted User

    Deleted User

    Guest

    It's easy to make mistakes these days ;). So much stress.