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.
I’m not sure your question is clear - what is that screenshot supposed to illustrate?
I apologize. I’ve now updated the OP with a lot better description.
Anyone?
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.
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);
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.
Ok. I’ll use the code feature next time then. The main problem was that the function:
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.
Vector3’s have three component values, namely x,y, and z.
So:
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…
It’s easy to make mistakes these days ;). So much stress.